整合ehcache
本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存
ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
-
导入依赖
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
整合ehcache必须要导入它的依赖。 -
yml配置
需要说明的是
config:classpath:/ehcache.xml可以不用写,因为默认就是这个路径。但ehcache.xml必须有。
1spring: 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已配置。
1import com.example.ehcache.dao.PersonRepository; 2import com.example.ehcache.entity.Person; 3import com.example.ehcache.service.DemoService; 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.cache.annotation.CacheConfig; 6import org.springframework.cache.annotation.CacheEvict; 7import org.springframework.cache.annotation.CachePut; 8import org.springframework.cache.annotation.Cacheable; 9import org.springframework.stereotype.Service; 10 11/** 12 * @author 李振 13 * @date 2018/12/27 14 * 15 */ 16@Service 17@CacheConfig(cacheNames = {"myCache"}) 18public 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} 61
整合完毕!
别忘了在启动类开启缓存!
源码地址:https://gitee.com/cnovel/demo
本文同步分享在 博客“吟风者”(JianShu)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。