Balazs Balazs - 4 years ago 173
Java Question

Using multiple cache implementations with Spring Cache

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.

Answer Source

Which implementation is needed based on the cache name?

Not based on the cache name but yes based on CacheManager its 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 on Cache name only then you can multiple CacheManager.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download