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";
}
usage:
assertEquals("api", service.getApi()); //OK
assertEquals("url", service.getUrl()); //FAILURE. this returns also "api"
So, why does @Cachable not create a cache result by method name if method signature does not contain any input parameters?
Try this
@Cacheable(value = CACHE, key = "#root.method.name")
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.
You can use two cache with different name:
@Bean
public ConcurrentMapCache cache() {
return new ConcurrentMapCache(CACHE_API, CACHE_URL);
}
@Cacheable(CACHE_API)
public String getApi() {
return "api";
}
@Cacheable(CACHE_URL)
public String getUrl() {
return "url";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With