好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

@Cacheable 拼接key的操作

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

?

1

2

3

4

5

@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表达式,如果要设置常量,则需要用''包裹,如:

?

1

@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作为属性解析

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/qq_24084605/article/details/81003369

查看更多关于@Cacheable 拼接key的操作的详细内容...

  阅读:12次