I'm using Spring Boot caching support in my web application and I set Caffeine as cache provider.
I have several caches in my project, most of them have common configuration, but for two specific caches I need to set different parameters.
In my application.properties
I have something similar:
spring.cache.cache-names=a-cache,b-cache,c-cache, ...
spring.cache.caffeine.spec=maximumSize=200,expireAfterWrite=3600s
This for common caches. Then I'd like to extend this configuration with custom params.
This post explains how to configure caches via @Configuration
class, but using this method I completely override the common configuration.
What I need is something like:
@Configuration
public class CacheConfiguration {
@Autowired
private CacheManager cacheManager;
@Bean
public CacheManager cacheManager(Ticker ticker) {
CaffeineCache c1 = new CaffeineCache("my-custom-cache", Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(400)
.build());
// ...
cacheManager.setCaches(Arrays.asList(..., c1, ... )); // here I'd like to add custom caches...
return cacheManager;
}
}
But declaring a new CacheManager
bean, the "original" cacheManager
is not autowired...
Is there a way to implement what I need?
CachingConfigurerSupport
class, but it simply returns a nullCacheManager
, not very useful to me. I'm currently trying to configure multiple cache managers, but it seem a bit overwhelming to follow this approach to achieve what I need... Thanks – Danais