当前位置:主页 > 软件编程 > JAVA代码 >

@Cacheable 拼接key的操作

时间:2021-05-13 08:13:32 | 栏目:JAVA代码 | 点击:

我就废话不多说了,大家还是直接看代码吧~

@Cacheable(value = "page_user",key ="T(String).valueOf(#page).concat('-').concat(#pageSize)",unless = "#result=null")//由于page是int型,concat要求变量必须为String,所以强转一下
@Override
public List<SysUserEntity> page(int page, int pageSize) {
  return userMapper.page(page,pageSize);
}

补充:记一个spring缓存Cacheable注解key设置问题

spring的Cacheable注解用来设置缓存,其中的key属性为spel表达式,如果要设置常量,则需要用''包裹,如:

@Cacheable(value = CacheConstant.APPLICATION,key = "'id_map'")

此处的"'id_map'"代表key设置了一个常量,如果没有'',则会报错

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'lang_code_map' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public or not valid?

意思为在缓存表达式根对象上找不到指定属性,说明如果不加'',则id_map作为属性解析

您可能感兴趣的文章:

相关文章