@在没有输入参数的方法上可用吗? [英] @Cachable on methods without input parameters?
本文介绍了@在没有输入参数的方法上可用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 @ org.springframework.cache.annotation.Cachable
批注时遇到问题:
I'm having a problem with @org.springframework.cache.annotation.Cachable
annotation:
@Bean
public ConcurrentMapCache cache() {
return new ConcurrentMapCache(CACHE);
}
@Cacheable(CACHE)
public String getApi() {
return "api";
}
@Cacheable(CACHE)
public String getUrl() {
return "url";
}
用法:
assertEquals("api", service.getApi()); //OK
assertEquals("url", service.getUrl()); //FAILURE. this returns also "api"
那么,如果方法签名不包含任何输入参数,为什么@Cachable不按方法名称创建缓存结果?
So, why does @Cachable not create a cache result by method name if method signature does not contain any input parameters?
推荐答案
尝试一下
@Cacheable(value = CACHE, key = "#root.method.name")
您需要告诉spring使用方法名称作为键.您可以为此使用SPEL.这是 doc 与其他各种选择.
You need to tell spring to use method name as key. You can use SPEL for this. Here is the doc with various other options.
这篇关于@在没有输入参数的方法上可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文