Spring @Cacheable注解的unless参数使用

Spring @Cacheable注解的unless参数使用,第1张

Spring @Cacheable注解的unless参数使用

工程使用spring缓存,但是发现再返回空的情况下也被缓存了,导致有数据后再查询还是返回被缓存的空值。

解决办法:使用unless参数

unless英文是除非的意思,意思就是 除了这个条件成立都缓存,又或者这个条件成立就不缓存

举例:

@ResponseBody
	@Cacheable(value = "custom_analyze", keyGenerator = "cacheKeyGenerator", condition = "#noCache!=true",
			unless = "#result.totalCount == 0")
	@CachePut(value = "custom_analyze", keyGenerator = "cacheKeyGenerator", condition = "#noCache==true")
	public Object sheetData(HttpServletRequest request,@Param("noCache")Boolean noCache) {
		baseRet br = new baseRet();
		Map paramMap = RequestParamUtil.getRequestParam(request,true);
		br.setAddData(mainService.getSheetData(paramMap, br));
		br.setSuccess(true);
		br.setMsg("查询成功!");
		return JsonpFactory.parse(request.getParameter("callback"), br);
	}

 我这个方法返回的是json字符串,而json返回的格式为:

如果totalCount为0的我就不缓存,unless参数里的 #result为方法的返回值,所以判断

result.totalCount==0 即可,返回条数为0则不缓存

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存