一、Spring Cache是什么?
它利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了。而且Spring Cache也提供了很多默认的配置,用户可以3秒钟就使用上一个很不错的缓存功能。
工作流程:
- 使用Spring Cache分为很简单的三步:添加依赖(springboot依赖包内置),开启缓存,加缓存注解。
- 每次调用该方法时,都会检查缓存以查看调用是否已经运行并且不必重复。虽然在大多数情况下,只声明了一个缓存,但注释允许指定多个名称,以便使用多个缓存。在这种情况下,在调用该方法之前检查每个缓存 - 如果至少命中一个缓存,则返回关联的值。
- 会触发一个后置处理,这会扫描每一个spring bean,查看是否已经存在缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。
特性:
-
缓存数据是存在redis
-
默认永不过期
-
key-value键、值队存储
二、使用步骤
1.开启基于注解的缓存
- 在启动类添加以下注解
@EnableCaching
2.配置缓存
- 在需要缓存数据的方法上面添加
@Cacheable
注解,即可缓存这个方法的返回值。
@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator", /*cacheNames = API_DETAIL_KEY_PREFIX, key = "target.redisApiDetailKeyPrefix + ':' + #appCode",*/ unless = "#result == null")
public OpenApiDetailVo findByAppCode(String appCode) {
return openApiAuthService.queryDetail(appCode);
}
三、解决方案
方案一:通过编写config设置缓存相关项
- 要指定 key 的过期时间,只需要getRedisCacheConfigurationMap方法中添加就可以。
@Configuration
public class RedisCacheConfig {
@Bean
public KeyGenerator simpleKeyGenerator() {
return (o, method, objects) -> {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(o.getClass().getSimpleName());
stringBuilder.append(".");
stringBuilder.append(method.getName());
stringBuilder.append("[");
for (Object obj : objects) {
stringBuilder.append(obj.toString());
}
stringBuilder.append("]");
return stringBuilder.toString();
};
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个
this.getRedisCacheConfigurationMap() // 指定 key 策略
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000));
redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
).entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
- 下面给出三种案例
// 3000秒
@Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator")
// 18000秒
@Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator")
// 600秒,未指定的key,使用默认策略
@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator")
方案二:通过配置文件
spring:
# maximumSize:配置缓存的最大条数,当快要达到容量上限的时候,缓存管理器会根据一定的策略将部分缓存项移除。
# expireAfterAccess:配置缓存项的过期机制,缓存项固定30秒将会过期,从而被移除。
cache:
caffeine:
spec: maximumSize=500, expireAfterAccess=30s
type: caffeine