无参数方法的@Cacheble注释
无参数方法的@Cacheble注释| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 17756753| 无参数方法的@Cacheble注释| 2023-10-11T11:52:00| | BlogPost|
@Cacheable(value="usercache", key = "mykey") public string sayHello(){ return "test" }
或者
@Override
@Cacheable(key = "#root.methodName + '['+#quantity+']'")
public List<CrownEntity> findHotDtl(int quantity) {
return crownDao.findHotDtl(quantity);
}
SpringBoot 缓存之 @Cacheable 详细介绍-CSDN博客
@Cacheable 、 @CachePut 、@CacheEvict 注解 - 乐之者v - 博客园 (cnblogs.com)
SpringCache之@Cacheable注解的说明使用_cacheable cachenames_ybb_ymm的博客-CSDN博客
无参方法,设置Cache的key值时,报错
rg.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field ‘mykey’ cannot be found on object of type ‘org.springframework.cache.interceptor.CacheExpressionRootObject’ - maybe not public?
It seems that Spring doesn’t allow you to provide a static text for the cache key in the SPEL, and it doesn’t include as default the name of the method on the key, so, you could be in a situation when two methods using the same cacheName and without a key would potentially cache different results with the same key.
解决方案
@Cacheable(value="usercache", key = "#root.methodName") public string sayHello(){ return "test" }
或
public static final String MY_KEY = "mykey";@Cacheable(value="usercache", key = "#root.target.MY_KEY")
public string sayHello(){
return "test"
}
参考:
无参数方法的@Cacheble注释(@Cacheble annotation on no parameter method)
Spring Cache key生成策略, 不要想当然认为是全类名+方法+参数