6

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.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
Balazs
  • 185
  • 1
  • 3
  • 10
  • With [`CompositeCacheManager`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/support/CompositeCacheManager.html) it should be possible, but you will possibly have to disabled Boot's autoconfiguration (I am not that familiar with Boot). – Pavel Horal Feb 01 '17 at 17:47
  • Check this answer - http://stackoverflow.com/a/21992641/865403 – Pavel Horal Feb 01 '17 at 17:54

3 Answers3

13

Which implementation is needed based on the cache name?

Not based on the cache name, but yes - based on the CacheManager it is possible. Declare one of them as @Primary CacheManager, as follows:

@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;
    }

}

and specify it at class level as:

@Service
@CacheConfig(cacheManager="hazelcastCacheManager")
public class EmployeeServiceImpl implements IEmployeeService {

}

or at method level as:

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Override
    @Cacheable(value = "EMPLOYEE_", key = "#id", cacheManager= "guavaCacheManager")
    public Employee getEmployee(int id) {
        return new Employee(id, "A");
    }

}

If you have to stick with Cache name only, then you can multiple CacheManager.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • I followed your solution but got this exception on startup: `java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary (or give it the name 'cacheManager') or declare a specific CacheManager to use, that serves as the default one.` I tried to use the name the error message mentions but it didn't help. Any idea on that? – Balazs Feb 02 '17 at 09:43
  • 1
    Thanks for the help, it works perfectly! It's a shame that Spring doesn't cover this part properly in their docs as it's fairly simple once you get some guidelines. – Balazs Feb 02 '17 at 10:45
  • @Arpit , @Balazs can we use both these cache managers in one method `@Cachable` ? – Soumitri Pattnaik Mar 28 '17 at 10:48
  • @SoumitriPattnaik no, only one at a time, as cacheManager is a `String` instance in `@Cachable` and not `Array`. – Arpit Aggarwal Mar 28 '17 at 11:31
  • As hazelcastCacheManager is annotated with @Primary so no need to specify cacheManager= "hazelcastCacheManager" specifically. – Myth Oct 15 '19 at 16:48
2

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:

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

@GuavaCacheable
@Service
public class MyServiceImpl implements MyService {
}

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.

isaolmez
  • 1,015
  • 11
  • 14
  • hello, I used this way. But still, Spring cannot identify which cache manager to autowire into my service class. any help please? – vigamage Dec 20 '18 at 13:00
0

One can consider using

https://github.com/yatharthamishra0419/multimodule-cache

library for support of multiple caching system, this library is extensible,multi-module aware and support for various caching systems can be added in the framework.This also includes a annotation through which multiple sources can be used

yathartha
  • 472
  • 5
  • 8