We use Spring cache framework for caching, and we'd like to able to support multiple namespaces for caches, such as "book", or "isbn", with the cache namespaces being configurable, rather than hardcoded in the class, like, instead of having
@Cacheable({ "book","isbn"})
public Book findBook(ISBN isbn) {...}
we want to be able somehow inject the cache name from a properties file, so that the cache name can be dynamically set, like:
@Cacheable({ #cachename1, #cachename2})
public Book findBook(ISBN isbn) {...}
I'm using SpEL here, but don't know if this is doable at all.
you can also use the cache directly, makes all even more predictable
etc ..
Going off smartwjw's answer...
I was looking to have cacheNames resolved via spring environment variables, such as
@Cacheable("${my.config.property.name}")
. I accomplished this via a customCacheResolver
And then of course you must configure it as THE
CacheResolver
to use with a@Configuration
that extendsorg.springframework.cache.annotation.CachingConfigurerSupport
.Spring 4.1 introduces
CacheResolver
, and use your self definedCacheResolver
to selectCache
then can be dynamic. spring 4.1 cache impovementsNope, dynamic (SpEL or otherwise) expressions are not supported for the cache name (
value
) property of the @Cacheable annotation. You would have to implement your own version of theorg.springframework.cache.annotation.SpringCacheAnnotationParser
and get it injected into the framework.