cacheable中的condition和unless
一、其中condition是对入参进行判断,符合条件的缓存,不符合的不缓存。
@Cacheable的condition 属性能使用的SpEL语言只有#root和获取参数类的SpEL表达式,不能使用返回结果的#result 。
所以 condition = "#result != null"
会导致所有对象都不进入缓存,每次操作都要经过数据库。
@GetMapping("/{id}")
@Cacheable(value = "userCache",key = "#id",condition = "#result != null")
public User getById(@PathVariable Long id){
User user = userService.getById(id);
return user;
}
二、其中unless是对出参进行判断,符合条件的不缓存,不符合的缓存。
解决方案: