在插入缓存时,会有<key,value>,我们希望value=null时,不要存入缓存,怎么实现呢?
使用 unless="#result == null"注解就可以,代码示例如下:
@Cacheable(value="defaultCache", key="#pk", unless="#result == null")
public Person findPerson(int pk) {
return getSession.getPerson(pk);
}
参考文档:
2. https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache-annotations-cacheable-default-key (这个是Spring Cache Annotation的官方文档,值得一看)