Soumitri Pattnaik
Soumitri Pattnaik

Reputation: 3556

Multiple cache implementations at method level in Spring Boot

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?

Upvotes: 2

Views: 705

Answers (1)

cruftex
cruftex

Reputation: 5723

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.

Upvotes: 1

Related Questions