一,问题定位:
项目中使用Caffeine作为本地缓存,配置expireAfterAccess(最后一次写入或访问后经过固定时间过期)过期时间值2分钟,示例代码如下:
@Cacheable(key = "'findResult:'+#no")
public Object findXXXResult(String no) {
Object object= null;
object = selectByNo(no);
return object;
}
1,第一次查询结果是空值,返回结果空值(null),Caffeine会将该null值进行缓存。
2,后续在过期时间有效期内,再根据同一个no去查询,直接使用本地缓存,返回 null;导致后续流程无法进行;
二,解决办法
1,expireAfterAccess改为expireAfterWrite(最后一次写入后经过固定时间过期);会在固定时间过期后,删除key;
2,在@Cacheable增加标签unless,unless=“#result==null”。
@Cacheable(key = "'findResult:'+#no",unless="#result==null")
public Object findXXXResult(String no) {
Object object= null;
object = selectByNo(no);
return object;
}