spring cache问题记录

1.是否可以设置过期时间 timeout ttl

对于单个key设置过期时间 需要自定义CacheManager, 见3中的问题
spring boot 1版本可以重写RedisCacheManager#computeExpiration
spring boot 2版本方案 :[参考]https://cloud.tencent.com/developer/article/1497599

2. @Cacheable不生效?

返回null, 没有保存到redis, 再次查询时以为没走AOP aspect
(见3中的问题)
另外一种是方法内部调用,不会走aop

3. 是否可以保存null value

redis是不支持保存Null的, 但是spring-cache可以把null设置成byte 0, 反序列化时再判断如果为0就setnull

需要设置RedisCacheManager cacheNullValues = true, 见构造方法, 默认cacheNullValues =null是不支持保存null value的

null value的序列化工具也不能使用下图中的一个,可以使用GenericJackson2JsonRedisSerializer或者fastjson实现

StringRedisSerializer默认实现只能序列化String, 会有很多的classCastException (还不容易定位到底是哪出错了), 最好也自己实现
ExtStringRedisSerializer implements RedisSerializer<Object>

4. cacheable解释

cachenames生成key的前缀, key指定具体的缓存id(可以使用springEL表达式获取查询参数的field)

redis中的示例 : 中间有个冒号

猜你喜欢

转载自www.cnblogs.com/yszzu/p/11657034.html