前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

步骤:

1. pom文件中加 maven jar包:

 <!-- ehcache 缓存 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency>
 <!--开启缓存支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>

2. 新增一个配置文件 ehcache.xml,放在resource 下面,springboot会自动扫描 :

<?xml version="1.0" encoding="UTF-8"?>
<ehcache><!--ehcache 缓存--><cache name="department" maxElementsInMemory="1000" />
</ehcache>

更多配置参考:

<ehcache><!--ehcache 缓存--><cache name="department"maxElementsInMemory="100000"eternal="true"overflowToDisk="true"maxElementsOnDisk="10000000"diskPersistent="true"memoryStoreEvictionPolicy="LRU"/><defaultCacheeternal="false"maxElementsInMemory="10000"overflowToDisk="false"diskPersistent="false"timeToIdleSeconds="0"timeToLiveSeconds="600"memoryStoreEvictionPolicy="LRU"/></ehcache>

3.  在 main 方法上加上注解 @EnableCaching,开启缓存的使用:

@EnableCaching // 开启缓存使用
@SpringBootApplication
public class Application { // extends SpringBootServletInitializerpublic static void main(String[] args) {SpringApplication application = new SpringApplication(Application.class, "classpath*:/spring/security-*.xml");application.setWebEnvironment(true);application.run(args);}/***  HOW TO MAKE A SPRING BOOT JAR INTO A WAR TO DEPLOY ON TOMCAT*/
//  @Override
//  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//      // Customize the application or call application.sources(...) to add sources
//
//      application.sources(Application.class);
//      return application;
//  }}

4. 在方法中运用注解,实现缓存的 增、删、改、查

只要在方法上加上对应注解就可以了。

@Cacheable 查: 如果有就直接缓存中取 没有就数据库查并放入缓存。加上这个注解,调用这个方法就可以取到缓存中的值。

@CacheEvict 新增、删除、修改 :会自动清除缓存中内容。加上这注解,对数据库的update、add、delete操作都会清除对应缓存。

如:缓存名为“ department ”,当调用此方法时会先判断是否有缓存。有则不进入方法,直接返回缓存中的值。无缓存名为 “department” 的缓存才会进入方法内部,执行数据库查询。

    @Override@Cacheable(value = "department")public List<Department> getDepartmentList() {System.out.println("--------------------------------缓存查询:查查查-树树树\n");List<Department> departmentList = departmentRepository.findAll(new Sort(Sort.Direction.ASC, "id"));return (List<Department>) buildDepartmentTree(departmentList, null);}

可以给缓存中的具体项起个键值:key

  @Override@Cacheable(value = "department", key = "#departmentId")public Object findOne(String departmentId) {return departmentRepository.findOne(departmentId);}

当缓存key没有全部命中时,要确保缓存全部清除,就要加上

 allEntries = true ,默认为false 
  @Override@Transactional(readOnly = false)@CacheEvict(value = "department", allEntries = true) // 新增、删除、修改 all is it.public Object updateDepartmentById(String id, Department departmentDto) {Department department = departmentRepository.findOne(id);if (department == null) {return "不存在该部门";}BeanHelper.mapPartOverrider(department, departmentDto);departmentRepository.save(department);return department;}

spring提供了4个注解来声明缓存规则(又是使用注解式的AOP的一个生动例子),如表。

事实上,新增、删除、修改都可以用@CacheEvict ,不建议使用 @ CachePut ,用法完全如上查的方法,只是注解名字不一样。

// 查:存key为cache_department 的数据缓存到departmentList中,如果没有指定key则方法参数作为key保存到缓存中。department只是缓存的名字。
//不指定 key 会默认使用参数名或者方法名,作为缓存的key。

5. 测试

第一次访问是没有缓存的,执行sql从数据库查,执行了查询方法,输出写在方法中的输出语句。

第二次访问,已有缓存,不进入方法,直接从缓存得数据并作为方法的返回值,不运行sql。如下:

springboot 缓存ehcache的简单使用相关推荐

  1. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  2. 页面缓存 ehcache(简单的)

    今天有空学习下页面缓存,我的例子是最简单的...: 1.下载ehcache-core  ehcache-web jar包. 2.web.xml: <filter> <filter-n ...

  3. springboot+mybatis集成自定义缓存ehcache用法笔记

    今天小编给大家整理了springboot+mybatis集成自定义缓存ehcache用法笔记,希望对大家能有所办帮助! 一.ehcache介绍 EhCache 是一个纯Java的进程内缓存管理框架,属 ...

  4. springboot整合ehcache+redis实现双缓存

    在一些对并发业务要求较高的场景下,对页面数据的响应是个急需解决的问题,对后端来说,ehcache+redis实现双缓存是解决这一问题的不错思路,而且在不少的电商项目中得到了很好的验证,但我在网上搜寻资 ...

  5. java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法

    java相关:springboot使用GuavaCache做简单缓存处理的方法 发布于 2020-3-29| 复制链接 摘记: 问题背景 实际项目碰到一个上游服务商接口有10秒的查询限制(同个账号). ...

  6. SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)

    1.说明 Spring定义了CacheManager和Cache接口, 用来统一不同的缓存技术, 例如JCache,EhCache,Hazelcast,Guava,Redis等. 本文通过Spring ...

  7. Springboot缓存实战笔记之概念篇

    看过好多博客,在介绍Springboot缓存时,直接讲解使用,第一步把冰箱门打开...之类的,并没有对缓存概念做一些常规介绍,任何技术首先要做的就是去了解概念,本篇基于我自身阅读过的一些书籍,自身觉得 ...

  8. springboot整合ehcache使用

    springboot整合ehcache使用 其实本地缓存的解决方案也有很多种,像Ehcache,GuavaCache,JCache等目前Ehcache 是现在最流行的纯Java开源缓存框架,配置简单. ...

  9. java 项目做多级缓存_【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)...

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

最新文章

  1. 【数字信号处理】傅里叶变换性质 ( 傅里叶变换频移性质示例 | PCM 音频信号处理 | 使用 matlab 进行频移操作 )
  2. Java动态代理类使用
  3. R6034--ARCENGINE
  4. python idle在哪_python安装包里idle在哪
  5. 基于Html5的移动端开发框架的研究
  6. python登录网站 爬虫_Python爬虫如何使用Cookies登录网站
  7. android Activity 之间传递复杂对象
  8. C++ opengl 纹理生成
  9. Crust Network将于1月14日12点开始暂停HTTP版本应用
  10. 灰色按钮激活程序的原理 (学习)
  11. 《数学之美》--自用
  12. 学习oracle之后的感悟,学习培训后的收获和感想
  13. 【解决报错】failed to obtain JDBC Connection
  14. Windows中的SID详解
  15. 纯CSS实现的3D翻页效果
  16. 银行从业资格考试通过后如何申请证书
  17. Urban Traffic System 创建行人路线
  18. MYSQL高可用环境搭建
  19. 机架式服务器物理尺寸,1U机架式服务器机箱尺寸长宽高
  20. 1. 什么是ThingsBoard

热门文章

  1. android 设置view最大高度,android-在RecyclerView上设置最大高度
  2. elasticsearch date_Elasticsearch在日志分析领域应用和运维实践
  3. unity3d collider自动调整大小_自动网格组合建模工具Unity游戏素材资源
  4. LeetCode每日打卡 - 汉明距离
  5. Maven超详细配置
  6. CCNA-第十篇-VLAN-下
  7. 【FZU - 1759】Super A^B mod C (数论,快速幂,快速乘,欧拉降幂,指数循环节,模板)
  8. Apollo进阶课程㊳丨Apollo平台的快速入门
  9. java 垃圾回收 null_java方法中把对象置null,到底能不能加速垃圾回收
  10. bread是可数还是不可数_可数名词不可数名词分不清?出题老师告诉你方法