设置过期时间没有效果,需要重新测试一遍,
测试过的数据都 写在注释里了,
// sync 异步执行必须是只有一个缓存标记 不能这样value={"VersionModel","versiontest"}一起使用
@Override
//@Cacheable(value={"VersionModel","versiontest"},key="\"applicationList\"",sync =true) //多个缓存标记用数组方式组合,删除任意一个缓存标记都会更新缓存,达到不同表的更新都能反映到缓存上
@Cacheable(value="VersionModel#${select.cache.timeout:1800}",key="\"applicationList\"",sync = true)
//timeout时间秒: 设置缓存过期时间.异步执行防止缓存失效后同一时间大量请求落到数据库,这个过期时间其实就是lock加锁,并不是KEY消失
//缓存失效,第一种方式 @Cacheable(value = "VersionModel#${select.cache.timeout:1000}#${select.cache.refresh:600", key = "")//example 是cache容器名字 #后边的是SpEL表达式
//缓存失效,第二种方式 @Cacheable(value = {"VersionModel#5#2"},key ="#id") 在缓存即将过期时主动刷新缓存,缓存失效前2秒内命中,主动刷新缓存,重置失效时间
@Autowired
VersionDao versionDao;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
//@CachePut(value="VersionModel",key="#mv.getApplicationId()")//仅仅清空KEY = mv.getApplicationId()的缓存,其实这种对于获取台账数据是错误
@CacheEvict(value="VersionModel",allEntries=true)
public int saveVersion(VersionModel mv) {
// TODO Auto-generated method stub
logger.info("清空VersionModel缓存, method=saveVersion");
return versionDao.saveVersion(mv);
}
// sync 异步执行必须是只有一个缓存标记 不能这样value={"VersionModel","versiontest"}一起使用
@Override
//@Cacheable(value={"VersionModel","versiontest"},key="\"applicationList\"",sync =true) //多个缓存标记用数组方式组合,删除任意一个缓存标记都会更新缓存,达到不同表的更新都能反映到缓存上
@Cacheable(value="VersionModel#${select.cache.timeout:1800}",key="\"applicationList\"",sync = true) //timeout时间秒: 设置缓存过期时间.异步执行防止缓存失效后同一时间大量请求落到数据库,这个过期时间其实就是lock加锁,并不是KEY消失
//缓存失效,第一种方式 @Cacheable(value = "VersionModel#${select.cache.timeout:1000}#${select.cache.refresh:600", key = "")//example 是cache容器名字 #后边的是SpEL表达式
//缓存失效,第二种方式 @Cacheable(value = {"VersionModel#5#2"},key ="#id") 在缓存即将过期时主动刷新缓存,缓存失效前2秒内命中,主动刷新缓存,重置失效时间
public List<VersionModel> versionList(HashMap<String, Object> map) {
// TODO Auto-generated method stub
logger.info("输出的都是没有走缓存");
List<VersionModel> vmlist = versionDao.versionList(map);
return vmlist;
}
@Override
@Cacheable(value="VersionModel",key="#str")
public VersionModel getVersion(String str) {
// TODO Auto-generated method stub
logger.info("从VersionModel拿出缓存 key=#applicationId,method=getVersion");
return versionDao.getVersion(str);
}
@Override
@CacheEvict(value="versiontest",allEntries=true)// 清空VersionModel 缓存
public void removeVersion(Long[] str) {
// TODO Auto-generated method stub
logger.info("把VersionModel缓存清空 ,method=removeVersion");
versionDao.removeVersion(str);
}
//@CacheEvict, beforeInvocation=false表示在方法执行之后调用(#result能拿到返回值了);且判断condition,如果返回true,则移除缓存;
//@CacheEvict(value = "user", key = "#user.id", beforeInvocation = false, condition = "#result.username ne 'zhang'")
//public User conditionDelete(final User user)
参考配置的过期时间