SoFunction
Updated on 2025-03-08

@Cacheable The operation of splicing key

I won't say much nonsense, let's just read the code~

@Cacheable(value = "page_user",key ="T(String).valueOf(#page).concat('-').concat(#pageSize)",unless = "#result=null")// Since page is int type, concat requires that the variable must be String, so force it to turn it@Override
public List<SysUserEntity> page(int page, int pageSize) {
  return (page,pageSize);
}

Replenish:Note a spring cache cached cacheable annotation key setting problem

The Cacheable annotation of spring is used to set cache, where the key attribute is a spel expression. If you want to set a constant, you need to wrap it with '', such as:

@Cacheable(value = ,key = "'id_map'")

"'id_map'" here means that the key has set a constant. If there is no '', an error will be reported.

: EL1008E: Property or field 'lang_code_map' cannot be found on object of type '' - maybe not public or not valid?

Meaning that the specified attribute cannot be found on the cache expression root object, indicating that if '' is not added, id_map will be parsed as the attribute.

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.