2

What I want is, two different cache implementations (let's say Redis and EhCache) on one method. Meaning @Cacheable method should cache both Redis and EhCache.

Is it even possible?

4
  • Why would the cache be in one of them and not both of them? Sorry its unclear what you are after. Commented Mar 28, 2017 at 10:34
  • Just saying, the cache should be in both of them @SMA Commented Mar 28, 2017 at 10:35
  • its still unclear why would you do that. You are replicating the same data across cache's and then you are asking cache to get value? One way i could say is create a class and decorate it with redis and ehcache instance. And add method's of cache like get/put... Commented Mar 28, 2017 at 10:37
  • @SMA what I want is, one distributed case (primary) and a local cache (secondary) configuration. My goal is to cache the data in both the caches, so if the distributed cache serer is down at least I get the data back from the local cache. Commented Mar 28, 2017 at 10:42

1 Answer 1

1

Option 1: Stack the caches. Configure the local cache in Spring. Then wire in the distributed cache via a CacheLoader/CacheWriter. Consistency needs to be carefully evaluated. E.g. if an update goes to the distributed cache, how do you invalidate the local caches? That is not so trivial. Maybe it is easy and not needed for your data, maybe it is near to impossible.

Option 2: Go with a distributed cache which provides a so called near cache. That is actually the combination you want to do by yourself, but combined in a product. I know that hazelcast and Infinispan offer a near cache. However, your mileage may vary regarding consistency and resilience. I know that hazelcast just recently enhanced the near cache, so it is consistent.

Interesting use case and actually a common problem. Further thoughts and discussion highly appreciated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you input, I am looking into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.