Spring自带缓存
如果没有使用缓存中间件,Spring Boot会使用默认的缓存,只需要启用即可
自带缓存启用注解
@EnableCaching
缓存配置详解
//用在类上
@CacheConfig(cacheNames = "bank") 用在类上,方法中则可以省略cacheNames 配置
// 用在方法上
@Cacheable: 先判断有没有缓存,有就取缓存,否则查库后再存入缓存,一般用在get 方法上(不支持设置缓存时间)
@CachePut: 操作后结果存入缓存 ,一般用在update上
@CacheEvict: 清除缓存 ,一般用在delete
@Caching 多个Cache组合使用
@Caching(
put = {
@CachePut(value = "bank",key="#bank.bankId"),
@CachePut(value = "bank",key="#bank.bankNo")
}
)
public BankVO save(){}
自定义缓存注解(@Caching组合,会让方法上的注解显得整个代码比较乱)
@Caching(
put = {
@CachePut(value = "bank",key="#bank.bankId"),
@CachePut(value = "bank",key="#bank.bankNo")
}
)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface UserSaveCache {
}
value/cacheNames 缓存的名字,必须指定至少一个, 也可以放在CacheConfig()中
key 缓存的key,可以为空,如果指定要按照SpEl表达式编写,如果不指定,则按照所有参数进行组合
condition 缓存的条件,可以为空,使用SpEL编写,返回true、false,只有为true才进行缓存
allEntries 是否清空所有缓存内容,默认为false,如果指定为true,则方法调用后立即清空所有缓存
beforeInvocation 是否在方法执行前就清空,默认为false(如果抛了异常则不会清空缓存),如果指定为true,则在方法还没有执行的时候就清空缓存
以下是相关代码
@Configuration
@EnableCaching //开启缓存,如果没有使用缓存中间件会使用Spring自带的缓存,否则使用中间件缓存,这里就会使用redis缓存中间件
public class CacheConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
RedisSerializer jackson2JsonRedisSerializer = jsonSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
/**
* RedisTemplate默认使用的序列化机制是JdkSerializationRedisSerializer,这里我们是用Jackson2JsonRedisSerializer
* @return
*/
private RedisSerializer jsonSerializer() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
return jackson2JsonRedisSerializer;
}
@Bean
public RedisUtil redisUtils(RedisTemplate<String, Object> template) {
return new RedisUtil(template);
}
/**
* key的生成策略()
* @return
*/
@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();
};
}
/********************以下代码解决@Cacheable不支持设置缓存过期的问题**************************/
/**
*
* @param redisConnectionFactory
* @return
*/
@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<>();
// 需要作缓存在这里加上就加一个put即可
redisCacheConfigurationMap.put("bankId", this.getRedisCacheConfigurationWithTtl(120)); // 120秒后失效
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer())
).entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
业务代码中使用
@Service
// @CacheConfig(cacheNames = "bank") //如果使用该注解,方法中则可以省略cacheNames /value 配置
@Slf4j
public class BankServiceImpl implements IBankService {
@Cacheable(value = "bankId",keyGenerator = "simpleKeyGenerator")
public BankVO getBank(Long bankId) {
log.info("获取用户银行卡,{}",bankId);
BankVO bankVO = new BankVO();
UserBank bank = bankDomain.getBank(bankId);
if(null != bank){
BeanUtils.copyProperties(bank,bankVO);
}
return bankVO;
}
}