0

Good evening all,

I'm new to Spring, let along Spring's caching. That said, I have a group of Maven projects all under the umbrella of a project, each of which requires some amount of caching. That said, my initial attempt at caching with multiple cache managers was failing... in some research I read some about using a custom CacheResolver and binding multiple cacheManagers within that. This thread spawned my interest.

Based on the answers there, I had some questions and was advised to post it in a new question. The gist of my question(s) is:

First off, is there an advantage to having multiple cacheManagers. For example on the project I'm working on, I originally thought we would need multiple managers, but I think if we were to put a singular manager in a -common maven project that all of our other projects use, then we could get away with the one (and it manages all of our caches (30+)).

Secondly, in the above example, let's assume I declare multiple cacheManagers in CustomCacheManager. I don't quite follow on how I determine which manager to use?

Thanks in advance!

vergessen
  • 41
  • 1
  • 7
  • According to this: https://docs.spring.io/spring/docs/4.3.15.RELEASE/spring-framework-reference/html/cache.html - an easy way to determine which cacheManager will be used is to declare it on each @Cacheable annotation you add your app... – moilejter Aug 18 '18 at 02:36

1 Answers1

2

Declare one cache manager as primary. Here is an example for reference:

@Configuration
@EnableCaching
@PropertySource(value = { "classpath:/cache.properties" })
public class CacheConfig {

    @Bean
    @Primary
    public CacheManager hazelcastCacheManager() {
        ClientConfig config = new ClientConfig();
        HazelcastInstance client = HazelcastClient.newHazelcastClient(config);
        return new HazelcastCacheManager(client);
    }

    @Bean
    public CacheManager guavaCacheManager() {
         GuavaCacheManager cacheManager = new GuavaCacheManager("mycache");
           CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
           .maximumSize(100)
           .expireAfterWrite(10, TimeUnit.MINUTES);
           cacheManager.setCacheBuilder(cacheBuilder);
           return cacheManager;
    }

}

Then specify it class level:

@Service
@CacheConfig(cacheManager="hazelcastCacheManager")
public class PolicyServiceImpl implements PolicyService {

}

You can also put it to method level:

@Service
public class PolicyServiceImpl implements PolicyService {

    @Override
    @Cacheable(value = "POLICY_", key = "#id", cacheManager= "guavaCacheManager")
    public Policy getPolicyDetails(int id) {
        return new Policy(id, "IX4546");
    }
}

Also another way of mentioning at method level or class level would be:

@CacheConfig(cacheManager = "ehCacheManager")
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface EhCacheable {
}

@EhCacheable
@Service
public class PolicyServiceImpl implements Policy {
}

You can also refer this link for custom cache resolver: https://github.com/isaolmez/spring-cache-samples/tree/master/spring-cache-custom

JJJ
  • 32,902
  • 20
  • 89
  • 102
mindSet
  • 231
  • 2
  • 10