@Cacheable在DAO层不生效:
因为想先简单写个测试类试一下缓存是否配置正确是否可用,直接在Controller层调用DAO层,DAO层代码如下:
@Cacheable(value = "USER_INFO", key = "#p0", unless = "null == #result")
UserInfo selectByPhone(String phone);
怎么修改配置都没用,在Controller层加@Cacheable注解有用,判断出了不是配置的问题。
最后加了Service层,好了。
本来想偷个懒结果反而浪费了更多时间。
总结一句就是不能直接在Controller层调用DAO层,否则@Cacheable注解无法生效。
默认两个冒号:
看了下源码长这样:
static CacheKeyPrefix simple(){
return (name) ->{
return name + "::";
}
}
这个只要在原来的RedisCacheConfiguration配置加一句就可以了,如下:
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHour(1)
// 这句,设置为单引号
.computePrefixWith(name - > name + ":")
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
另附keyGenerator写法
@Configuration
public class RedisKeyGenerator {
@Bean("myKeyGenerator")
public KeyGenerator myKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return params[0];
}
};
}
}
网友评论