I am using autoconfigured https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-caching-provider-simple with some @Cacheable
and @EnableCaching
annotations. I have a couple of named caches. Caching works as expected.
Now I would like to add TTL and maybe some other customizations to my caches. I tried to replace the default ConcurrentHashMap used by ConcurrentMapCacheManager with a Guava cache. I followed suggestion from https://stackoverflow.com/a/8181992/1469083 and did this using ConcurrentMapCacheFactoryBean:
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanA() {
ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();
cacheFactoryBean.setName("A");
cacheFactoryBean.setStore(CacheBuilder.newBuilder()
.expireAfterWrite(DURATION, TimeUnit.SECONDS)
.build()
.asMap());
return cacheFactoryBean;
}
My cache names are A, B or C
@Cacheable("A")
Since I need 3 caches, it looks like I need to configure 3 ConcurrentMapCacheFactoryBeans with different cache names?
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanA() {
cacheFactoryBean.setName("A");
...
}
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanB() {
cacheFactoryBean.setName("B");
...
}
@Bean
public ConcurrentMapCacheFactoryBean cacheFactoryBeanC() {
cacheFactoryBean.setName("C");
...
}
is this proper way to do it? Is there easier way to configure ConcurrentMapCache store? This way I lose the automatic cache creation that I had earlier, where I could just add annotation @Cacheable("D")
to some method, and cache D would automatically be created.
I know Spring Boot has replaced Guava caching support with autoconfigured Caffeine support, which has properties for TTL, and I could switch to that track also. But I like the flexibility of this approach and also am curious.