SpringBoot 如何快速使用 Caffeine 缓存?

存储 存储软件
引入 Caffeine 和 Spring Cache 依赖,使用 SpringCache 注解方法实现缓存。SpringCache帮我们封装了Caffeine pom文件引入。

[[433190]]

引言

前面我们有学习Caffeine 《本地缓存性能之王Caffeine》,并且也提到SpringBoot默认使用的本地缓存也是Caffeine啦,今天我们来看看Caffeine如何与SpringBoot集成的。

集成caffeine

caffeine与SpringBoot集成有两种方式:

  • 一种是我们直接引入 Caffeine 依赖,然后使用 Caffeine 方法实现缓存。相当于使用原生api
  • 引入 Caffeine 和 Spring Cache 依赖,使用 SpringCache 注解方法实现缓存。SpringCache帮我们封装了Caffeine pom文件引入
  1. <dependency> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-cache</artifactId> 
  4. </dependency> 
  5. <dependency> 
  6.     <groupId>com.github.ben-manes.caffeine</groupId> 
  7.     <artifactId>caffeine</artifactId> 
  8.     <version>2.6.0</version> 
  9. </dependency> 

第一种方式

首先配置一个Cache,通过构造者模式构建一个Cache对象,然后后续关于缓存的增删查都是基于这个cache对象。

  1. @Configuration 
  2. public class CacheConfig { 
  3.     @Bean 
  4.     public Cache<String, Object> caffeineCache() { 
  5.         return Caffeine.newBuilder() 
  6.                 // 设置最后一次写入或访问后经过固定时间过期 
  7.                 .expireAfterWrite(60, TimeUnit.SECONDS) 
  8.                 // 初始的缓存空间大小 
  9.                 .initialCapacity(100) 
  10.                 // 缓存的最大条数 
  11.                 .maximumSize(1000) 
  12.                 .build(); 
  13.     } 

第一种方式我们就一一不介绍了,基本上就是使用caffeineCache来根据你自己的业务来操作以下方法

这种方式使用的话是对代码有侵入性的。

第二种方式

  • 需要在SpingBoot启动类标上EnableCaching注解,这个玩意跟很多框架都一样,比如我们肴集成dubbo也需要标上@EnableDubbole注解等。
  1. @SpringBootApplication 
  2.   @EnableCaching 
  3.   public class DemoApplication { 
  4.       public static void main(String[] args) { 
  5.           SpringApplication.run(DemoApplication.class, args); 
  6.       } 
  • 在application.yml配置我们的使用的缓存类型、过期时间、缓存策略等。
  1. spring: 
  2.   profiles: 
  3.     active: dev 
  4.   cache: 
  5.     type: CAFFEINE 
  6.     caffeine: 
  7.       spec: maximumSize=500,expireAfterAccess=600s 

如果我们不习惯使用这种方式的配置,当然我们也可以使用JavaConfig的配置方式来代替配置文件。

  1. @Configuration 
  2. public class CacheConfig { 
  3.         @Bean 
  4.         public CacheManager cacheManager() { 
  5.             CaffeineCacheManager cacheManager = new CaffeineCacheManager(); 
  6.             cacheManager.setCaffeine(Caffeine.newBuilder() 
  7.                     // 设置最后一次写入或访问后经过固定时间过期 
  8.                     .expireAfterAccess(600, TimeUnit.SECONDS) 
  9.                     // 初始的缓存空间大小 
  10.                     .initialCapacity(100) 
  11.                     // 缓存的最大条数 
  12.                     .maximumSize(500)); 
  13.             return cacheManager; 
  14.         } 

接下来就是代码中如何来使用这个缓存了。

  1. @Override 
  2. @CachePut(value = "user"key = "#userDTO.id"
  3. public UserDTO save(UserDTO userDTO) { 
  4.     userRepository.save(userDTO); 
  5.     return userDTO; 
  6.  
  7. @Override 
  8. @CacheEvict(value = "user"key = "#id")//2 
  9. public void remove(Long id) { 
  10.     logger.info("删除了id、key为" + id + "的数据缓存"); 
  11.  
  12. @Override 
  13. @Cacheable(value = "user",key = "#id"
  14. public UserDTO getUserById(Long id) { 
  15.     return userRepository.findOne(id); 

上述代码中我们可以看到有几个注解@CachePut、@CacheEvict、@Cacheable我们只需要在方法上标上这几个注解,我们就能够使用缓存了,我们分别来介绍下这几个注解。

@Cacheable

@Cacheable它是既可以标注在类上也可以标注在方法上,当它标记在类上的时候它表述这个类上面的所有方法都会支持缓存,同样的 当它作用在法上面时候它表示这个方法是支持缓存的。比如上面我们代码中的getUserById这个方法第一次缓存里面没有数据,我们会去查询DB,但是第二次来查询的时候就不会走DB查询了,而是直接从缓存里面拿到结果就返回了。

value 属性

  • @Cacheable的value属性是必须指定的,其表示当前方法的返回值是会被缓存在哪个Cache上的,对应Cache的名称。

key

  • @Cacheable的key 有两种方式一种是我们自己显示的去指定我们的key,还有一种默认的生成策略,默认的生成策略是SimpleKeyGenerator这个类,这个生成key的方式也比较简单我们可以看下它的源码:
  1. public static Object generateKey(Object... params) { 
  2.         // 如果方法没有参数 key就是一个 new SimpleKey() 
  3.   if (params.length == 0) { 
  4.    return SimpleKey.EMPTY; 
  5.   } 
  6.   // 如果方法只有一个参数 key就是当前参数 
  7.   if (params.length == 1) { 
  8.    Object param = params[0]; 
  9.    if (param != null && !param.getClass().isArray()) { 
  10.     return param; 
  11.    } 
  12.   } 
  13.   // 如果key是多个参数,key就是new SimpleKey ,不过这个SimpleKey对象的hashCode 和Equals方法是根据方法传入的参数重写的。 
  14.   return new SimpleKey(params); 
  15.  } 

上述代码还是非常好理解的分为三种情况:

  • 方法没有参数,那就new使用一个全局空的SimpleKey对象来作为key。
  • 方法就一个参数,就使用当前参数来作为key
  • 方法参数大于1个,就new一个SimpleKey对象来作为key,new 这个SimpleKey的时候用传入的参数重写了SimpleKey的hashCode和equals方法, 至于为啥需要重写的原因话,就跟Map用自定义对象来作为key的时候必须要重写hashCode和equals方法原理是一样的,因为caffein也是借助了ConcurrentHashMap来实现,

小结

上述代码我们可以发现默认生成key只跟我们传入的参数有关系,如果我们有一个类里面如果存在多个没有参数的方法,然后我们使用了默认的缓存生成策略的话,就会造成缓存丢失。或者缓存相互覆盖,或者还有可能会发生ClassCastException 因为都是使用同一个key。比如下面这代码就会发生异常(ClassCastException)。

  1. @Cacheable(value = "user"
  2.   public UserDTO getUser() { 
  3.       UserDTO userDTO = new UserDTO(); 
  4.       userDTO.setUserName("Java金融"); 
  5.       return userDTO; 
  6.   } 
  7.   @Cacheable(value = "user"
  8.   public UserDTO2 getUser1() { 
  9.       UserDTO2 userDTO2 = new UserDTO2(); 
  10.       userDTO2.setUserName2("javajr.cn"); 
  11.       return userDTO2; 
  12.   } 

所以一般不怎么推荐使用默认的缓存生成key的策略。如果非要用的话我们最好自己重写一下,带上方法名字等。类似于如下代码:

  1. @Component 
  2. public class MyKeyGenerator extends SimpleKeyGenerator { 
  3.  
  4.     @Override 
  5.     public Object generate(Object target, Method method, Object... params) { 
  6.         Object generate = super.generate(target, method, params); 
  7.         String format = MessageFormat.format("{0}{1}{2}", method.toGenericString(), generate); 
  8.         return format; 
  9.     } 

自定义key

我们可以通过Spring的EL表达式来指定我们的key。这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”这也是我们比较推荐的做法:

  1. @Cacheable(value="user"key="#id"
  2.  public UserDTO getUserById(Long id) { 
  3.      UserDTO userDTO = new UserDTO(); 
  4.      userDTO.setUserName("java金融"); 
  5.      return userDTO; 
  6.  } 
  7.  @Cacheable(value="user"key="#p0"
  8.  public UserDTO getUserById1(Long id) { 
  9.      return null
  10.  } 
  11.  @Cacheable(value="user"key="#userDTO.id"
  12.  public UserDTO getUserById2(UserDTO userDTO) { 
  13.      return null
  14.  } 
  15.  @Cacheable(value="user"key="#p0.id"
  16.  public UserDTO getUserById3(UserDTO userDTO) { 
  17.      return null
  18.  } 

@CachePut

@CachePut指定的属性是和@Cacheable一样的,但是它们两个是有区别的,@CachePut标注的方法不会先去查询缓存是否有值,而是每次都会先去执行该方法,然后把结果返回,并且结果也会缓存起来。

为什么是这样的一个流程我们可以去看看它的源码关键代码就是这一行,

  1. Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class)); 

当我们使用方法上有@Cacheable注解的时候再contexts里面会把CacheableOperation加入进去,只有contexts.get(CacheableOperation.class)取到的内容不为空的话,才会去从缓存里面取内容,否则的话cacheHit会直接返回null。至于contexts什么时候加入CacheableOperation的话我们看下SpringCacheAnnotationParser#parseCacheAnnotations这个方法就会明白的。具体的源码就不展示了,感兴趣的可以自己去翻。

@CacheEvict

把缓存中数据删除,用法跟前面两个注解差不多有value和key属性,需要注意一点的是它多了一个属性beforeInvocation

  • beforeInvocation 这个属性需要注意下它的默认值是false,false代表的意思是再执调用方法之前不删除缓存,只有方法执行成功之后才会去删除缓存。设置为true的话调用方法之前会去删除一下缓存,方法执行成功之后还会去调用删除缓存这样就是双删了。如果方法执行异常的话就不会去删除缓存。
  • allEntrie 是否清空所有缓存内容,默认值为 false,如果指定为 true,则方法调用后将立即清空所有缓存

@Caching

这是一个组合注解集成了上面三个注解,有三个属性:cacheable、put和evict,分别用于来指定@Cacheable、@CachePut和@CacheEvict。

小结

第二种方式是侵入式的,它的实现原理也比较简单就是通过切面的方法拦截器来实现,拦截所有的方法,它的核心代码如下:看起来就跟我们的业务代码差不了多少,感兴趣的也可以去瞅一瞅。

  1. if (contexts.isSynchronized()) { 
  2.    CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next(); 
  3.    if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) { 
  4.     Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT); 
  5.     Cache cache = context.getCaches().iterator().next(); 
  6.     try { 
  7.      return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker)))); 
  8.     } 
  9.     catch (Cache.ValueRetrievalException ex) { 
  10.      // The invoker wraps any Throwable in a ThrowableWrapper instance so we 
  11.      // can just make sure that one bubbles up the stack. 
  12.      throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause(); 
  13.     } 
  14.    } 
  15.    else { 
  16.     // No caching required, only call the underlying method 
  17.     return invokeOperation(invoker); 
  18.    } 
  19.   } 
  20.  
  21.  
  22.   // Process any early evictions 
  23.   // beforeInvocation 属性是否为true,如果是true就删除缓存 
  24.   processCacheEvicts(contexts.get(CacheEvictOperation.class), true
  25.     CacheOperationExpressionEvaluator.NO_RESULT); 
  26.  
  27.   // Check if we have a cached item matching the conditions 
  28.   Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class)); 
  29.  
  30.   // Collect puts from any @Cacheable miss, if no cached item is found 
  31.   List<CachePutRequest> cachePutRequests = new LinkedList<>(); 
  32.   if (cacheHit == null) { 
  33.    collectPutRequests(contexts.get(CacheableOperation.class), 
  34.      CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests); 
  35.   } 
  36.  
  37.   Object cacheValue; 
  38.   Object returnValue; 
  39.  
  40.   if (cacheHit != null && !hasCachePut(contexts)) { 
  41.    // If there are no put requests, just use the cache hit 
  42.    cacheValue = cacheHit.get(); 
  43.    returnValue = wrapCacheValue(method, cacheValue); 
  44.   } 
  45.   else { 
  46.    // Invoke the method if we don't have a cache hit 
  47.    returnValue = invokeOperation(invoker); 
  48.    cacheValue = unwrapReturnValue(returnValue); 
  49.   } 
  50.  
  51.   // Collect any explicit @CachePuts 
  52.   collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests); 
  53.  
  54.   // Process any collected put requests, either from @CachePut or a @Cacheable miss 
  55.   for (CachePutRequest cachePutRequest : cachePutRequests) { 
  56.    cachePutRequest.apply(cacheValue); 
  57.   } 
  58.  
  59.   // Process any late evictions 
  60.   processCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue); 
  61.  
  62.   return returnValue; 
  63.  } 

结束

由于自己才疏学浅,难免会有纰漏,假如你发现了错误的地方,还望留言给我指出来,我会对其加以修正。

感谢您的阅读,十分欢迎并感谢您的关注。 

站在巨人的肩膀上摘苹果: https://www.cnblogs.com/fashflying/p/6908028.html#!comments

责任编辑:武晓燕 来源: java金融
相关推荐

2021-07-11 18:06:18

缓存过期淘汰

2022-03-15 08:22:31

Ehcachespring缓存

2022-03-31 13:58:37

分布式SpringRedis

2024-01-19 14:03:59

Redis缓存系统Spring

2012-02-08 11:01:53

HibernateJava

2018-09-12 19:46:53

数据库MySQLRedis

2023-06-28 08:25:14

事务SQL语句

2023-05-05 18:38:33

多级缓存Caffeine开发

2024-01-03 21:50:32

缓存机制请求

2022-03-18 13:59:46

缓存RedisCaffeine

2010-01-08 14:41:24

JSON 缓存数据

2011-06-01 09:03:12

Android 缓存

2017-03-22 10:06:40

ubuntuceph集群

2020-01-10 15:42:13

SpringBootRedis数据库

2020-06-29 07:43:12

缓存RedisSpringBoot

2021-02-01 08:00:00

vimLinux加密

2019-09-02 14:51:33

2023-02-14 07:47:20

SpringBootEhcache

2020-10-27 14:15:42

SpringBoot

2021-08-05 16:10:03

进程缓存缓存服务Java
点赞
收藏

51CTO技术栈公众号