暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringBoot使用Ehcache进行缓存

钟陵虞子期 2019-08-13
632


SpringBoot使用Ehcache进行缓存

Ehcache其实是一个缓存管理器,Ehcache支持本地缓存,Ehcache还支持分布式的缓存

在我们开发中经常碰到一个方法总是执行的很慢,但是这个方法对数据的实时准确度要求不是很高的时候,我们可以使用缓存技术来优化。

1. 导入jar包

  1. <!-- Spring Boot 缓存支持启动器 -->

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-cache</artifactId>

  5. </dependency>

  6. <!-- Ehcache 坐标 -->

  7. <dependency>

  8. <groupId>net.sf.ehcache</groupId>

  9. <artifactId>ehcache</artifactId> <!--ehcache的版本 如果没有指定,springboot 会自动指定-->

  10. </dependency>

2. 编写ehcache.xml 缓存配置文件(放在resources里面)

  1. ehcache配置文件中元素说明

ehcach.xml配置文件主要参数的解释,其实文件里有详细的英文注释

//DiskStore 配置

cache 数据的存放目录(其实就是 <diskStorepath="Java.io.tmpdir"/>
path的取值),主要的值有

  • user.home - 用户主目录

  • user.dir - 用户当前的工作目录

* java.io.tmpdir - Default temp file path 默认的 temp 文件目录

// Ehcache 的三种清空策略

  • FIFO,first in first out,这个是大家最熟的,先进先出。

  • LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来

最少被使用的。如上面所讲,缓存的元素有一个 hit 属性,hit 值最小的将会被清出缓存。

  • LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量

满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时

间最远的元素将被清出缓存。

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <ehcache>

  3. <!-- 下面这个会报错

  4. <?xml version="1.0" encoding="UTF-8"?>

  5. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

  7. updateCheck="false"> -->

  8. <diskStore path = "Java.io.tmpdir"/>

  9. <defaultCache

  10. eternal="false" <--意味着该缓存会死亡-->

  11. maxElementsInMemory="900"<--缓存的最大数目-->

  12. overflowToDisk="false" <--内存不足时,是否启用磁盘缓存,如果为true则表示启动磁盘来存储,如果为false则表示不启动磁盘-->

  13.             diskPersistent="false"

  14. timeToIdleSeconds="0" <--当缓存的内容闲置多少时间销毁-->

  15. timeToLiveSeconds="60" <--当缓存存活多少时间销毁(单位是秒,如果我们想设置2分钟的缓存存活时间,那么这个值我们需要设置120)-->

  16. memoryStoreEvictionPolicy="LRU" /> <--自动销毁策略-->

  17. <!-- 这里的 users 缓存空间是为了下面的 demo 做准备 -->

  18. <cache

  19. name="data"

  20. eternal="false"

  21. maxElementsInMemory="200"

  22. overflowToDisk="false"

  23. diskPersistent="false"

  24. timeToIdleSeconds="0"

  25. timeToLiveSeconds="60"

  26. memoryStoreEvictionPolicy="LRU" />

  27. </ehcache>

  28. <!--<diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口) -->

  29. <!--<diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index -->

  30. <!--name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里) -->

  31. <!--maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 -->

  32. <!--maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况 -->

  33. <!--1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中 -->

  34. <!--2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素 -->

  35. <!--eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds -->

  36. <!--timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性 -->

  37. <!--即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除 -->

  38. <!--timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大 -->

  39. <!--即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除 -->

  40. <!--overflowToDisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中) -->

  41. <!--会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data -->

  42. <!--diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件 -->

  43. <!--这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存 -->

  44. <!--要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法 -->

  45. <!--diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,默认是120秒 -->

  46. <!--diskSpoolBufferSizeMB============设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB -->

  47. <!--memoryStoreEvictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存 -->

  48. <!--共有三种策略,分别为LRU(Least Recently Used 最近最少使用)、LFU(Less Frequently Used最不常用的)、FIFO(first in first out先进先出) -->

3. 在springboot配置文件中引入ehcache.xml配置

  1. # 在Springboot配置文件中把ehcache.xml配置进去;即在application.properties中加入以下配置代码

  2. spring.cache.ehcache.config=ehcache.xml

  3. # 在网上也看到要如下配置

  4. spring.cache.type=ehcache

  5. spring.cache.ehcache.config=classpath:ehcache.xml

  6. # 如果springboot配置文件为yaml文件, 则如下配置:

  7. #使用ehcache缓存配置

  8. spring:

  9. cache:

  10. type: ehcache

  11. ehcache:

  12. config: classpath:ehcache.xml

4. 在启动类中开启缓存

在启动类前加上@EnableCaching注解;这样的话,启动类启动时会去启动缓存启动器。

  1. @SpringBootApplication

  2. @EnableCaching // 开启缓存 启动类启动时会去启动缓存启动器。

  3. public class DemoApplication {

  4. public static void main(String[] args) {

  5. SpringApplication.run(DemoApplication.class, args);

  6. }

  7. }

5. 使用Ehcache的注解进行缓存

注意:

  • 缓存的数据要实现序列化接口Serializable,由于需要实体类支持缓存中的磁盘存储,所以需要实体类实现可序列化接口

缓存常用注解:

  • @CacheConfig

  • @Cacheable

  • @CachePut

  • @CacheEvict

  1. 1)@CacheConfig(cacheNames = {“lemonCache”})设置了ehcache的名称,这个名称就是ehcache.xml内的名称;(可以不指定,应为在yml 中已经制定了)

  2. 2)@Cacheable:应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调 用方法获取数据,然后把数据添加到缓存中,适用于查找;

  3. 3)@CachePut:主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。适用于更新和插入;

  4. 4)@CacheEvict:主要针对方法配置,能够根据一定的条件对缓存进行清空。适用于删除。

示例:

  1. @Service

  2. @Transactional //事务,表示该类下所有的都受事务控制

  3. public class userServiceImpl implements userService {

  4. @Autowired

  5. private userMapper usermapper;

  6. @Override

  7. @Cacheable(value="users")

  8. public user selectUserById(int id) {

  9. user user=this.usermapper.selectUserById(id);

  10. System.out.println("1111111111111111111111111");

  11. return user;

  12. }

  13. }

说明: @Cacheable可以标记在一个方法上,也可以标记在一个类上,当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。

@Cacheable可以指定三个属性,value、key和condition。

value属性指定cache的名称(即选择ehcache.xml中哪种缓存方式存储)

key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。我们也直接使用“#参数名”或者“#p参数index” (这里的参数指的是方法的形参名)
。下面是几个使用参数作为key的示例

  1. @Cacheable(value="users", key="#id")

  2. public User find(Integer id) {

  3. returnnull;

  4. }

  5. @Cacheable(value="users", key="#p0")

  6. public User find(Integer id) {

  7. returnnull;

  8. }

  9. @Cacheable(value="users", key="#user.id")

  10. public User find(User user) {

  11. returnnull;

  12. }

  13. @Cacheable(value="users", key="#p0.id")

  14. public User find(User user) {

  15. returnnull;

  16. }

最后,使用@CacheEvict清除缓存;

  1. @CacheEvict(value="users",allEntries=true)

  2. public void saveUsers(Users users) {

  3. this.usersRepository.save(users);

  4. }

 说明:@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。

其中value、key和condition的语义与@Cacheable对应的属性类似;allEntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。

当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率。

  1. package com.shyroke.service.impl;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.cache.annotation.CacheConfig;

  4. import org.springframework.cache.annotation.CacheEvict;

  5. import org.springframework.cache.annotation.CachePut;

  6. import org.springframework.cache.annotation.Cacheable;

  7. import org.springframework.stereotype.Repository;

  8. import org.springframework.stereotype.Service;

  9. import com.shyroke.dao.UserMapper;

  10. import com.shyroke.entity.UserBean;

  11. import com.shyroke.service.CacheUserService;

  12. @CacheConfig(cacheNames = "roncooCache")

  13. @Service

  14. public class CacheUserServiceImpl implements CacheUserService{

  15. @Autowired

  16. private UserMapper userMapper;

  17. @Cacheable(key = "#p0")

  18. @Override

  19. public UserBean getUserById(int id) {

  20. System.out.println("查询功能,缓存找不到,直接读库, id=" + id);

  21. return userMapper.getOne(id);

  22. }

  23. @CachePut(key = "#p0")

  24. @Override

  25. public UserBean update(UserBean user) {

  26. System.out.println("更新功能,更新缓存,直接写库, id=" + user);

  27. return userMapper.save(user);

  28. }

  29. @CacheEvict(key = "#p0")

  30. @Override

  31. public String deleteById(int id) {

  32. System.out.println("删除功能,删除缓存,直接写库, id=" + id);

  33. userMapper.delete(id);

  34. return "删除成功";

  35. }

  36. }

6. 将数据自定义进入缓存

  1. public class EHCacheUtils {

  2. /**

  3. * 设置缓存对象

  4. * @param cacheManager

  5. * @param key

  6. * @param object

  7. */

  8. public static void setCache(CacheManager cacheManager,String key,Object object){

  9. Cache cache = cacheManager.getCache("objectCache");

  10. Element element = new Element(key,object);

  11. cache.put(element);

  12. }

  13. /**

  14. * 从缓存中取出对象

  15. * @param cacheManager

  16. * @param key

  17. * @return

  18. */

  19. public static Object getCache(CacheManager cacheManager,String key){

  20. Object object = null;

  21. Cache cache = cacheManager.getCache("objectCache");

  22. if(cache.get(key)!=null && !cache.get(key).equals("")){

  23. object = cache.get(key).getObjectValue();

  24. }

  25. return object;

  26. }

  27. }

存入缓存方法如下:

  1. @Autowired

  2. private CacheManager cacheManager;

  3. //部分关键代码

  4. EHCacheUtils.setCache(cacheManager,"op",searchOP);

取出缓存方法如下:

  1. @Autowired

  2. private CacheManager cacheManager;

  3. Operator searchOP = (Operator) EHCacheUtils.getCache(cacheManager,"op");

问题:

  1. net.sf.ehcache(Ehcache 2.x)和org.ehcache(Ehcache 3.x)共存时引起的BUG

问题再现:Maven中同时存在如下依赖

  1. <!-- Ehcache 3.x -->

  2. <dependency>

  3. <groupId>org.ehcache</groupId>

  4. <artifactId>ehcache</artifactId>

  5. <version>3.6.3</version>

  6. </dependency>

  7. <!-- Ehcache 2.x -->

  8. <dependency>

  9. <groupId>net.sf.ehcache</groupId>

  10. <artifactId>ehcache</artifactId>

  11. <version>2.10.6</version>

  12. </dependency>

项目启动则会报

Caused by: net.sf.ehcache.CacheException: java.lang.annotation.IncompleteAnnotationException: org.terracotta.statistics.Statistic missing element type Caused by: java.lang.annotation.IncompleteAnnotationException: org.terracotta.statistics.Statistic missing element type

原因:net.sf.ehcache与org.ehcache存在jar包冲突 Ehcache官方issues:https://github.com/ehcache/ehcache3/issues/2354解决方法:去掉net.sf.ehcache或者org.ehcache其中一个

如果要使用Ehcache 3.x,则需要阻止Spring Boot将Hibernate配置为使用Ehcache 2.x进行缓存

  1. 对加入缓存方法的参数(并且这个参数要作为cache的key)传NULL值,出现的BUG

项目启动则会报

Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.lang.String com.gwm.service.impl.TestService.selectById(java.io.Serializable)] caches=[hour] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

调用方法:

  1. @Cacheable(value = "hour", key = "#id")

  2. public String selectById(Serializable id) {

  3. Long timestamp = System.currentTimeMillis();

  4. return timestamp.toString();

  5. }

出错原因

key中的id 没有传值,在调用方法时,不能传null值。

eg:

  1. Serializable id = "aa";

  2. String meg=testService.selectById(id); // 传上值!!!!!

  1. 在cache 中查询缓存数据时,key要和之前存进去的key的类型一致

demo:

  1. @Service

  2. public class EhcacheTest {

  3. private static Integer count = 0;

  4. @Autowired

  5. UserRepository userRepository;

  6. @Autowired

  7. CacheManager cacheManager;

  8. // key 为Long类型 值为1

  9. @Cacheable(value = "users",key="#id")

  10. public User findUserById(Long id) {

  11. Optional<User> user = userRepository.findById(id);

  12. System.out.println("加入缓存了 -- > " + count);

  13. count++;

  14. return user.get();

  15. }

  16. @Cacheable(value = "users",key="#name")

  17. public User findUserByName(String name) {

  18. User user = userRepository.findByUserName(name);

  19. System.out.println("加入缓存了 -- > " + count);

  20. count++;

  21. return user;

  22. }

  23. @CacheEvict(value="users",allEntries=true)

  24. public void clearCache() {

  25. System.out.println("清空缓存");

  26. }

  27. public User getCache(String key) {

  28. Cache cache = cacheManager.getCache(SystemConstant.CACHE_NAME);

  29. List keys = cache.getKeys();

  30. for (Object o : keys) {

  31. System.out.println(o.toString());

  32. }

  33. Element element = cache.get(key);

  34. User user = (User) element.getObjectValue();

  35. return user;

  36. }

  37. // 查询的时候一定要这个方法来查 如果使用参数为String ("1") 就会报空指针错误 因为会查不到key为“1”的值

  38. public User getCache2(Long key) {

  39. Cache cache = cacheManager.getCache(SystemConstant.CACHE_NAME);

  40. List keys = cache.getKeys();

  41. for (Object o : keys) {

  42. System.out.println(o.toString());

  43. }

  44. Element element = cache.get(key);

  45. User user = (User) element.getObjectValue();

  46. return user;

  47. }

  48. }

参考链接:

  • https://www.cnblogs.com/xzmiyx/p/9897623.html

  • https://www.cnblogs.com/xiaowangbangzhu/p/10558029.html

  • https://www.jianshu.com/p/05f3ede0b389

  • https://blog.csdn.net/qq_28143647/article/details/79789368

  • https://www.cnblogs.com/shyroke/p/8025604.html

  • https://blog.csdn.net/Lammonpeter/article/details/78602862

  • http://www.ehcache.org/apidocs/2.9/net/sf/ehcache/Ehcache.html#getKeys() ehcache API

  • How to Iterate on a cache entries 第二个链接也包含了spring下咋整的结论。

  • https://www.cnblogs.com/wchxj/p/8119528.html

文章转载自钟陵虞子期,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论