本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存

ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持RESTSOAP api等特点。

1、依赖导入

整合ehcache必须要导入它的依赖。

  1. <dependency>
  2. <groupId>net.sf.ehcache</groupId>
  3. <artifactId>ehcache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-cache</artifactId>
  8. </dependency>

2、yml文件

需要说明的是config:classpath:/ehcache.xml可以不用写,因为默认就是这个路径。但ehcache.xml必须有。

  1. spring:
  2. cache:
  3. type: ehcache
  4. ehcache:
  5. config: classpath:/config/ehcache.xml

3、ehcache.xml

resources目录下新建config文件夹,在文件夹中建立ehcache.xml文件。

  1. <ehcache>
  2.  
  3. <!--
  4. 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
  5. path:指定在硬盘上存储对象的路径
  6. path可以配置的目录有:
  7. user.home(用户的家目录)
  8. user.dir(用户当前的工作目录)
  9. java.io.tmpdir(默认的临时目录)
  10. ehcache.disk.store.dir(ehcache的配置目录)
  11. 绝对路径(如:d:\\ehcache)
  12. 查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
  13. -->
  14. <diskStore path="java.io.tmpdir" />
  15.  
  16. <!--
  17. defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
  18. maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
  19. eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
  20. timeToIdleSeconds:最大的发呆时间 /秒
  21. timeToLiveSeconds:最大的存活时间 /秒
  22. overflowToDisk:是否允许对象被写入到磁盘
  23. 说明:下列配置自缓存建立起600秒(10分钟)有效 。
  24. 在有效的600秒(10分钟)内,如果连续120秒(2分钟)未访问缓存,则缓存失效。
  25. 就算有访问,也只会存活600秒。
  26. -->
  27. <defaultCache maxElementsInMemory="10000" eternal="false"
  28. timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" />
  29.  
  30. <cache name="myCache" maxElementsInMemory="10000" eternal="false"
  31. timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" />
  32.  
  33. </ehcache>

4、使用缓存

@CacheConfig(cacheNames={“myCache”})设置ehcache的名称,这个名称必须在ehcache.xml已配置。

  1. import com.example.ehcache.dao.PersonRepository;
  2. import com.example.ehcache.entity.Person;
  3. import com.example.ehcache.service.DemoService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cache.annotation.CacheConfig;
  6. import org.springframework.cache.annotation.CacheEvict;
  7. import org.springframework.cache.annotation.CachePut;
  8. import org.springframework.cache.annotation.Cacheable;
  9. import org.springframework.stereotype.Service;
  10.  
  11. /**
  12. * @author 李振
  13. * @date 2018/12/27
  14. *
  15. */
  16. @Service
  17. @CacheConfig(cacheNames = {"myCache"})
  18. public class DemoServiceImpl implements DemoService {
  19. @Autowired
  20. private PersonRepository personRepository;
  21. /**
  22. * 注意:如果没有指定key,则方法参数作为key保存到缓存中
  23. */
  24. /**
  25. * @param person
  26. * @return
  27. * @CachePut缓存新增的或更新的数据到缓存,其中缓存的名称为people,数据的key是person的id
  28. */
  29. @CachePut(key = "#person.id")
  30. @Override
  31. public Person save(Person person) {
  32. Person p = personRepository.save(person);
  33. System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
  34. return p;
  35. }
  36.  
  37. /**
  38. * @param id
  39. * @CacheEvict从缓存people中删除key为id的数据
  40. */
  41. @CacheEvict
  42. @Override
  43. public void remove(Integer id) {
  44. System.out.println("删除了id,key为" + id + "的数据缓存");
  45. personRepository.delete(id);
  46. }
  47.  
  48. /**
  49. * @param person
  50. * @return
  51. * @Cacheable缓存key为person的id数据到缓存people中
  52. */
  53. @Cacheable(key = "#person.id")
  54. @Override
  55. public Person findOne(Person person) {
  56. Person p = personRepository.findOne(person.getId());
  57. System.out.println("为id,key为:" + p.getId() + "数据做了缓存");
  58. return p;
  59. }
  60. }

以上

原文出处:

吟风者,springboot使用ehcache缓存, https://www.jianshu.com/p/9e4eb5e5bc1b

Springboot使用ehcache缓存的更多相关文章

  1. 转载-Springboot整合ehcache缓存

    转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...

  2. springboot 整合ehcache缓存

    1.CacheManager Spring Boot默认集成CacheManager,如下包所示: 可以看出springboot自动配置了 JcacheCacheConfiguration. EhCa ...

  3. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

  4. springboot整合ehcache缓存失效

    最近做了个微信公众号后台,因为只是单应用就选用了ehcache来做本地缓存,主要是用于缓存微信的accece_token和jsapi_ticket.在使用ehcache的时候遇到了@Cacheable ...

  5. 【spring-boot】spring-boot 整合 ehcache 实现缓存机制

    方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种   引入 ehcach ...

  6. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存

    1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...

  7. SpringBoot中Shiro缓存使用Redis、Ehcache

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  8. Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细

    前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...

  9. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

随机推荐

  1. Java连载43-访问静态方法其他注意事项、static关键字

    一.其他注意点 1.对于带有static的方法,我们讲过需要用“类名.”的方式进行访问,但是其实我们使用“引用.”的方式也是可以进行访问这个方法的举例. package com.bjpowernode ...

  2. spring cloud 2.x版本 Hystrix Dashboard断路器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  3. CF 704 D. Captain America

    CF 704 D. Captain America 题目链接 题目大意:给出\(n\)个点的坐标,你要将每个点染成红色或者蓝色.染一个红色要付出\(r\)的代价,染一个蓝色要付出\(b\)的代价.有\ ...

  4. 前端笔记之React(一)初识React&组件&JSX语法

    一.React项目起步配置 官网:https://reactjs.org/ 文档:https://reactjs.org/docs/hello-world.html 中文:http://react.c ...

  5. 死磕 java同步系列之CountDownLatch源码解析

  6. C# - VS2019页面布局容器splitContainer和groupBox小结

    前言 在WinFrm应用程序中,产品的外观.布局将直接影响用户第一体验,所以对于开发者来说,在没有美工支持的前提下,应当注意系统页面的布局,本章主要讲解splitContainer和groupBox的 ...

  7. git遇到的错误和解决方法(长期更新)

    1:场景:将两个git合并成一个git url,由于项目超过100M,所以出现错误,以下是解决方案:

  8. Android SearchView不显示搜索icon

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/80 背景: 之前碰到了一个页面展示问题,SearchVie ...

  9. Linux中vim编辑命令

    vim 功能 : 一个强大的文本编辑器   语法格式 :vim [ 选项 ] / 路径 / 文本文件名 命令格式: vi [ 选项 ] [ 文件名 ]   +num 打开某个文件直接跳转到 num 行 ...

  10. PHP将字符串转数组

    explode(',',$arr_string) //将字符串转数组 $arr_string = '1,2,3'; $arr = explode(',',$arr_string); dump($arr ...