一、介绍
1、背景
项目中使用最多的缓存技术就是Redis
,用Redis就可以实现了,为什么需要使用spring cache?
先看下我们使用缓存步骤:
(1)查寻缓存中是否存在数据,如果存在则直接返回结果
(2)如果不存在则查询数据库,查询出结果后将结果存入缓存并返回结果
(3)数据更新时,先更新数据库
(4)然后更新缓存,或者直接删除缓存
可以看到逻辑都差不多,这样就出现大量重复的代码,而且缓存与业务耦合较深。Spring Cache
则解决了这个问题,它利用AOP
实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了。
2、简介
spring cache官网Cache Abstraction :: Spring Framework
spring cache支持spel表达式,
3、默认配置
在默认配置下,
springcache缓存的是用jdk序列化过的数据,我们通常是缓存Json字符串,因为使用Json能跨语言,跨平台进行交互,;
我们也可以修改他的默认配置,包括ttl(过期时间)、存储格式等。
4、使用方法
spring cache提供了注解式和编程式两种写法。具体集成&使用方法见下一篇。
二、spring cache相关注解
1、@CacheConfig
在类级别共享缓存的相同配置。如果一个类中,多个方法都有同样的 cacheName,keyGenerator,cacheManager 和 cacheResolver,可以直接使用 @CacheConfig 注解在类上声明,这个类中的方法都会使用@CacheConfig 属性设置的相关配置。
@Component
@CacheConfig(cacheNames = "mall_cache")
public class CacheComponent {
@Cacheable(key = "'perm-whitelist-'+#clientId", unless="#result == null")
public List<String> cacheWriteList(String clientId){
...
}
@Cacheable(key = "'perm-cutom-aci-' + #tenantId + '-' + #roleId + '-' + #tenantLevel + '-' + #subType", unless="#result == null")
public List<RequestDto> cacheRequest(Long tenantId,Long roleId,Integer tenantLevel,Integer subType){
...
}
}
2、@Cacheable
触发将数据保存到缓存的操作(启动缓存),有9个属性:value、 cacheNames、 key、 keyGenerator、 cacheManager、 cacheResolver、 condition、 unless、 sync。
2.1、value/cacheNames 属性
这两个属性代表的意义相同,根据@AliasFor注解就能看出来了。这两个属性都是用来指定缓存组件的名称,即将方法的返回结果放在哪个缓存中,属性定义为数组,可以指定多个缓存;
2.2、key
可以通过 key 属性来指定缓存数据所使用的的 key,默认使用的是方法调用传过来的参数作为 key。最终缓存中存储的内容格式为:Entry<key,value> 形式。
(1)如果请求没有参数:key=new SimpleKey();
(2)如果请求有一个参数:key=参数的值
(3)如果请求有多个参数:key=newSimpleKey(params); (你只要知道 key不会为空就行了)
key的实现有两种方式:
(1) SpEL 表达式
(2)使用 keyGenerator生成器的方式来指定 key,需要编写一个 keyGenerator ,将该生成器注册到 IOC 容器即可。
2.3、keyGenerator
key 的生成器。如果觉得通过参数的方式来指定比较麻烦,我们可以自己指定 key 的生成器的组件 id。key/keyGenerator属性:二选一使用。
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
import java.util.Arrays;
@Configuration
public class MyCacheConfig {
@Bean("myKeyGenerator")
public KeyGenerator keyGenerator(){
return new KeyGenerator(){
@Override
public Object generate(Object target, Method method, Object... params) {
return method.getName()+ Arrays.asList(params).toString();
}
};
}
/**
* 支持 lambda 表达式编写
*/
/*@Bean("myKeyGenerator")
public KeyGenerator keyGenerator(){
return ( target, method, params)-> method.getName()+ Arrays.asList(params).toString();
}*/
}
2.4、cacheManager
用来指定缓存管理器。针对不同的缓存技术,需要实现不同的 cacheManager,Spring 也为我们定义了如下的一些 cacheManger 实现()
2.5、cacheResolver
该属性,用来指定缓存解析器。使用配置同 cacheManager 类似(cacheManager指定管理器/cacheResolver指定解析器 它俩也是二选一使用)
2.6、condition
条件判断属性,用来指定符合指定的条件下才可以缓存。也可以通过 SpEL 表达式进行设置。这个配置规则和上面表格中的配置规则是相同的。
@Cacheable(value = "user",condition = "#id>0")//传入的 id 参数值>0才进行缓存
User getUser(Integer id);
@Cacheable(value = "user",condition = "#a0>1")//传入的第一个参数的值>1的时候才进行缓存
User getUser(Integer id);
@Cacheable(value = "user",condition = "#a0>1 and #root.methodName eq 'getUser'")//传入的第一个参数的值>1 且 方法名为 getUser 的时候才进行缓存
User getUser(Integer id);
也可以引用自定义Component组件的自定义方法
condition="@userKeyConstructor.isConditionPassing()"
2.7、unless
unless属性,意为"除非"的意思。即只有 unless 指定的条件为 true 时,方法的返回值才不会被缓存。可以在获取到结果后进行判断。比如判断结果的条数超过多少则不缓存,防止影响redis性能。
@Cacheable(value = "user",unless = "#result == null")//当方法返回值为 null 时,就不缓存
User getUser(Integer id);
@Cacheable(value = "user",unless = "#a0 == 1")//如果第一个参数的值是1,结果不缓存
User getUser(Integer id);
2.8、sync
该属性用来指定是否使用异步模式,该属性默认值为 false,默认为同步模式。异步模式指定 sync = true 即可,异步模式下 unless 属性不可用。
3、@CacheEvict
触发将数据从缓存删除的操纵(失效模式)。@CacheEvict 是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。属性有下面几个:
public @interface CacheEvict {
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
String key() default "";
String keyGenerator() default "";
String cacheManager() default "";
String cacheResolver() default "";
String condition() default "";
boolean allEntries() default false;
boolean beforeInvocation() default false;
}
其他的几个和@Cacheable的属性差不多,这里看下上面没有介绍过的:
3.1、allEntries:
是否需要清除缓存中的所有元素。默认为 false ,表示不需要。当指定了 allEntries 为 true 时,Spring Cache将忽略指定的key,删除缓存中所有键;被注解的方法抛异常也能执行。
3.2、beforeInvocation:
是否在方法执行成功之后触发键删除操作,这个属性决定了是先删缓存还是先更新db。
(1)默认false
默认是在对应方法成功执行之后触发的(即先更新db再删缓存),若此时方法抛出异常而未能成功返回,不会触发清除操作。为了避免多线程缓存一致性问题,beforeInvocation一般默认为false。
为了保证缓存正常被删除,处理方式:
注解式: 采用@CacheEvict注解并且自定义了keyGenerator,则需要在入参里面拿到所有组合key需要的字段,比如userId、userAccount均有缓存,如果@CacheEvict注解的接口只有userId参数会导致userAccountId缓存脏数据,应该从service根据userId查询UserDTO实体传入。如果在keyGenerator里面才根据id查询,因为db已经变更了(delete/update),可能拿不到或者拿到的是脏数据。
编程式: 需要在db变更的逻辑前拿到UserDTO实体。如在repository中增加一个公共的方法deleteList,这个方法里传参是List<Entity>, 由这个方法统一进行缓存的清理,注意: 同repository其他方法先查到list,然后再调用这个统一删除方法,调用时候要保证aop生效(public 方法,注入自身)。
根据redis 缓存一致性一文的分析,通常采用“先更新db再删除缓存”的方案,所以此项推荐使用false。
(2)指定该属性值为 true 时
Spring会在调用该方法之前清除缓存中的指定元素,即先删除缓存再更新db。
4、@CachePut
不影响方法执行更新缓存(双写模式)。
5、@Caching
组合以上多个操作(点击注解看源码就知道了,组合注解))。
public @interface Caching {
Cacheable[] cacheable() default {};
CachePut[] put() default {};
CacheEvict[] evict() default {};
}
@Caching 注解可以在一个方法或者类上同时指定多个Spring Cache相关的注解。
其拥有三个属性:cacheable、put 和 evict,分别用于指定@Cacheable、@CachePut 和 @CacheEvict。对于一个数据变动,更新多个缓存的场景,可以通过 @Caching 来实现:
@Caching(cacheable = @Cacheable(cacheNames = "caching", key = "#age"), evict = @CacheEvict(cacheNames = "t4", key = "#age"))
public String caching(int age) {
return "caching: " + age + "-->" + UUID.randomUUID().toString();
}
上面这个就是组合操作:从 caching::#age 缓存取数据,不存在时执行方法并写入缓存;删除缓存 t4::#age。
三、原理
流程说明:
CacheAutoConfiguration => RedisCacheConfiguration =>
自动配置了RedisCacheManager => 初始化所有的缓存 =>
每个缓存决定使用什么配置=>
=>如果RredisCacheConfiguration有就用已有的,没有就用默认配置(CacheProperties)
=>想改缓存的配置,只要给容器中放一个RredisCacheConfiguration即可
=>就会应用到当前RedisCacheManager管理的所有缓存分区中
1、CacheAutoConfiguration
缓存的自动配置,用的类型是redis所以看 RedisCacheConfiguration
2、CacheManager
缓存管理者,类型是redis
所以看 RedisCacheManager
3、CacheProperties
缓存默认配置
4、@CacheEvict
@CacheEvict
是通过AOP实现的,其中核心的类是CacheAspectSupport
四、重写
evict方法
实际使用一般都需要重写evict方法。
1、介绍
@CacheEvict只支持字符串类型的key,不支持key为集合,但是往往在清除缓存时需要清除很多key,再比如根据key模糊删除等也是不支持的。如果需要实现这样的功能,就需要自定义重写evict方法。实际使用一般都需要重写evict方法。
spring cache支持通过继承Cache
接口(使用redis,则对应RedisCache)和CacheManager(使用redis,则对应
RedisCacheManager)
接口来自定义缓存的行为,包括缓存的创建、缓存的清除等。
2、重写步骤
(1)自定义RedisCache
继承RedisCache类,重写evict方法
(2)自定义RedisCacheManager
自定义的RedisCache需要注册到RedisCacheManager才能生效,所以需要自定义
RedisCacheManager,继承RedisCacheManager即可
(3)在RedisConfig中管理自定义的RedisCacheManager
3、重写demo
(1)重写evict 方法
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import java.util.List;
@Slf4j
public class MyRedisCache extends RedisCache {
protected MyRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
super(name, cacheWriter, cacheConfiguration);
}
@Override
public void evict(Object key) {
if(key instanceof List<?>) {
//我这里采用了循环调用evict的方法,也可以自定义实现,通过操作redis来删除,如org.springframework.data.redis.connection.DefaultedRedisConnection#del 就提供了根据批量key来删除
List<String> keys = (List<String>) key;
keys.forEach( item -> super.evict(item));
}else{
super.evict(key);
}
}
}
注意:上面是为了实现效果,采用了循环调用evict的方法快速实现,也可以通过操作redis来删除,如org.springframework.data.redis.connection.DefaultedRedisConnection#del 就提供了根据批量key来删除 。
代码如下:
package com.pluscache.demo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Collection;
import java.util.List;
@Slf4j
public class MyRedisCache extends RedisCache {
private StringRedisTemplate redisTemplate;
protected MyRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
super(name, cacheWriter, cacheConfiguration);
}
private StringRedisTemplate getStringRedisTemplate(){
if(redisTemplate == null){
return ApplicationContextUtil.getBean(StringRedisTemplate.class);
}
return redisTemplate;
}
@Override
public void evict(Object key) {
log.info("key为:{}",key.toString());
if(key instanceof List<?>) {
List<String> keys = (List<String>) key;
//①直接循环调用evict
//keys.forEach( item -> super.evict(item));
//②通过connection批量删除key
byte[][] keysByte = serializeCacheKeys(keys);
this.redisTemplate = getStringRedisTemplate();
this.redisTemplate.execute(
(RedisCallback<Long>)
(connection) -> {
return connection.del(keysByte);
});
}else{
super.evict(key);
}
}
private byte[][] serializeCacheKeys(Collection<String> keys) {
byte[][] bytesKeys = new byte[keys.size()][];
int i = 0;
for (String key : keys) {
//加上前缀
String formatKey = this.getName()+"::"+key;
bytesKeys[i] = serializeCacheKey(formatKey);
i++;
}
return bytesKeys;
}
}
其中ApplicationContextUtil获取bean:
package com.pluscache.demo.config;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.util.Map;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext ;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return ApplicationContextUtil.applicationContext.getBean(name);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return applicationContext.getBeansOfType(clazz);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T extends Annotation> T getAnnotation(Object bean, Class<T> annotationClass) {
T annotation = bean.getClass().getAnnotation(annotationClass);
if (annotation == null) {
annotation = AopUtils.getTargetClass(bean).getAnnotation(annotationClass);
}
return annotation;
}
}
(2)
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class MyRedisCacheManager extends RedisCacheManager {
private final RedisCacheWriter cacheWriter;
private final RedisCacheConfiguration defaultCacheConfig;
public MyRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
super(cacheWriter, defaultCacheConfiguration);
this.cacheWriter = cacheWriter;
this.defaultCacheConfig = defaultCacheConfiguration;
}
public MyRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) {
super(cacheWriter, defaultCacheConfiguration, initialCacheNames);
this.cacheWriter = cacheWriter;
this.defaultCacheConfig = defaultCacheConfiguration;
}
public MyRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation, String... initialCacheNames) {
super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames);
this.cacheWriter = cacheWriter;
this.defaultCacheConfig = defaultCacheConfiguration;
}
/**
* 覆盖父类创建RedisCache,采用自定义的RedisCacheResolver
*/
@Override
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
return new MyRedisCache(name, cacheWriter, cacheConfig != null ? cacheConfig : defaultCacheConfig);
}
@Override
public Map<String, RedisCacheConfiguration> getCacheConfigurations() {
Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(getCacheNames().size());
getCacheNames().forEach(it -> {
RedisCache cache = MyRedisCache.class.cast(lookupCache(it));
configurationMap.put(it, cache != null ? cache.getCacheConfiguration() : null);
});
return Collections.unmodifiableMap(configurationMap);
}
}
(3)
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import java.time.Duration;
@Configuration
@EnableCaching
@EnableConfigurationProperties(RedisProperties.class)//开启属性绑定配置的功能
public class CacheConfig {
private RedisCacheConfiguration defaultCacheConfig() {
return RedisCacheConfiguration.defaultCacheConfig()
//缓存key的前缀
//.computePrefixWith(versionCacheKeyPrefix)
// key过期时间,60分钟
.entryTtl(Duration.ofMinutes(60));
// .disableCachingNullValues()
// Serialization & Deserialization when using annotation
//.serializeKeysWith(STRING_PAIR)
// .serializeValuesWith(FAST_JSON_PAIR);
//.serializeValuesWith(PROTO_STUFF_PAIR);
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
// 这里可以根据实际情况创建并返回RedisConnectionFactory的实现类,例如LettuceConnectionFactory或JedisConnectionFactory
return new LettuceConnectionFactory();
}
@Bean
public CacheManager cacheManager() {
RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory());
return new MyRedisCacheManager(cacheWriter,defaultCacheConfig());
}
}