I'm working on a Spring Boot
app where I need to use both distributed (e.g. Hazelcast
) and local (e.g. Guava
) caches. Is there a way to configure Spring Cache to use both when using @Cacheable
and decide which implementation is needed based on the cache name?
I tried with creating a configuration for both HZ and Guava defining the cache names inside, but Spring complains that it couldn't find the cache name that is supposed to handled by HZ. When I use exclusively HZ or Guava they work.
Not based on the cache name but yes based on CacheManager its possible, declare one of them as
Primary CacheManager
, as follows:And specify it at class level as:
Or at method level as:
If you have to stick on Cache name only then you can multiple CacheManager.
You have 2 options.
One is as @Arpit mentioned: Define multiple CacheManagers and specify it in either method-level annotations (@Cacheable, @CachePut, etc) or class-level annotations (@CacheConfig)
You can also create custom annotations:
And as the second option, you can create a custom cache resolver if your caching needs are complex.
You can look here for a custom CacheResolver that manages multiple CacheManagers and supports enabling/disabling. But for most cases, CacheResolver is overkill.