Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

11 篇文章 0 订阅

设置过期时间没有效果,需要重新测试一遍,

测试过的数据都 写在注释里了,

    // 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)   
	

 

 

参考配置的过期时间

https://blog.csdn.net/zyt807/article/details/82428615

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值