I have a default redis cache configuration in my application.yml:
cache:
type: redis
redis:
time-to-live: 7200000 # 2 hour TTL - Tune this if needed later
redis:
host: myHost
port: myPort
password: myPass
ssl: true
cluster:
nodes: clusterNodes
timeout: 10000
It works great and I don't want to create any custom cache manager for it.
However, there are some caches in my code where using redis is not necessary. For that reason, I want to make a second CacheManager that's a simple ConcurrentHashMap and specify it with @Cacheable
To do that I created a new CacheManager Bean:
@Configuration
@EnableCaching
@Slf4j
class CachingConfiguration {
@Bean(name = "inMemoryCache")
public CacheManager inMemoryCache() {
SimpleCacheManager cache = new SimpleCacheManager();
cache.setCaches(Arrays.asList(new ConcurrentMapCache("CACHE"));
return cache;
}
}
This causes the inMemoryCache to be my default cache and all my other @Cacheable() tried to use the inMemoryCache. I don't want the CacheManager bean that I created to be my default. Is there anyway I can specify that it's secondary and not prevent spring-cache for doing it's magic?
Thanks!
CacheManagerbean already defined in the context (i.e. the inMemoryCache), Spring BootCacheAutoConfigurationwill skip the creation ofRedisCacheManagerin this case. Therefore you'll have to set both Redis and In-Memory cache in yourCachingConfigurationclass, and then annotate your methods e.g. with@Cacheable(cacheManager="inMemoryCache", ...)to use in-memory cache. - IgorCacheManagerdefined in your Spring context, the auto-config will be skipped, and you'll have to configure bothCacheManagers manually. - Igor