时间:2023-03-01 15:10:51 | 栏目: | 点击:次
由于缓存的注解有类似的前缀,所以抽取出常量。
public static final String KEY_CERT_TYPE_CODE_PREFIX = "ec_cert_type:cert_type_code:";
@Override
@Cacheable(key = KEY_CERT_TYPE_CODE_PREFIX + "+#code")
public EcCertType loadCertTypeByCode(String code) {
// something
return type;
}
但是使用时,报错:
EL1041E: After parsing a valid expression, there is still more data in the expression
经修改,给常量加上单引号,使:不被SpEL解析,解决。
public static final String KEY_CERT_TYPE_CODE_PREFIX = "'ec_cert_type:cert_type_code:'";
@Override
@Cacheable(key = KEY_CERT_TYPE_CODE_PREFIX + "+#code")
public EcCertType loadCertTypeByCode(String code) {
// something
return type;
}

使用默认key生成器的话,缓存的对象需要实现toString()方法,里面拼接自己想要作为key的字段。

调用查询接口之后,缓存中的key如下:

其中TRANSFORMERS-ACT_DICT-KEY是定义的value值,表示放到哪个缓存中。

运行后key值如下
![]()
此时是以传递过来的Student对象中的id值作为key。

引用自定义的生成器

key值如下:
![]()