Manas Saxena
Manas Saxena

Reputation: 2365

Is it possible to use the cache I want with @Cacheable spring?

I have a custom cache created to suite my application.

I want to use the same for caching remote service calls, it is possible to do the same with @Cacheable?

As far as I explored, I did not find anything which allows @Cacheable to use a custom cache.

Upvotes: 1

Views: 1565

Answers (2)

John Blum
John Blum

Reputation: 8001

Your description of "custom" cache is very vague and can have different meanings depending on the context.

For instance:

  1. Do you mean you have built your own caching solution, or perhaps you are using a 3rd party caching solution for which there is no out-of-the-box caching provider implementation provided by either the core Spring Framework cache storage providers or by 1 of Spring Boots supported caching providers? If so then you see here. In a nutshell, you must provide an implementation of Spring's CacheManager and Cache interface to "adapt" your "custom" caching solution to fit with Spring's Cache Abstraction.

  2. Or, perhaps you mean you need some complex, rule-based "cache resolution logic" to determine the appropriate cache to use at runtime? If this is the case, see here.

Let's expand on #1 above, since this seems to be most likely your Use Case.

If you have a self-built, or 3rd party, "custom" caching solution, then of course you must adapt the caching provider's types in order to plugin it into the Spring Framework's caching infrastructure and use declarative annotation-based caching (e.g. @Cacheable).

It would be unreasonable to expect Spring to know how to interface with ever possible caching implementation available, or yet to be conceived.

So, the CacheManager and Cache interface allows you to adapt your caching solution/provider of choice, even if you baked it yourself, and still use Spring's caching infrastructure, such as the declarative annotation-based caching (i.e. using @Cacheable, @CachePut, and so on, even JCache (JSR-107) annotations, if you like).

This is the fundamental basis for plugging in and using different caching providers, under-the-hood, in Spring's Cache Abstraction itself.

For example, check out the Redis implementation, specifically the RedisCacheManager and the RedisCache classes, which implements Spring's CacheManager and Cache interfaces, respectively, or the Pivotal GemFire implementation and specifically the GemfireCacheManager and GemfireCache classes, which again, implements Spring's CacheManager and Cache interfaces, respectively.

This same pattern is used for all caching solutions, which can possibly be plugged into Spring's Cache Abstraction.

Each caching provider will use a different notion of what the "cache" for that particular provider actually is. In Pivotal GemFire the cache is a Region. And so, GemFire's Region type must be adapted by implementing Spring's Cache interface so that a Region can serve as a "cache" in Spring's Cache Abstraction. The same is true for any provider, even something as simple as the java.util.concurrent.ConcurrentHashMap, see here and then here.

So, if you have your own cache implementation...

  1. First adapt your "custom" cache by implementing the methods in the Cache interface, providing a CustomCache class. The methods in the Cache interface will delegate to your actual caching solution.

  2. Then provide an implementation of the CacheManager, e.g. CustomCacheManager that know how to a) configure your caching solution, and b) create instance of CustomCache for all the caches identified in Spring's caching annotations.

Let's say you have a CustomerService method that know how to find Customers by their Accounts...

@Service
class CustomerService {

  @Cacheable("Customers")
  Customer findBy(Account account) {
    ...
  }
}

Then, your CustomCacheManager will need to provide a CustomCache instance for the "Customers" cache identified in the @Cacheable annotation on the CustomerService.findBy(:Account) method shown above.

To use your CustomCacheManager implementation in your Spring application, then simply declare a bean with a "name" of "cacheManager" that returns an instance of your CustomCacheManager class and "enable" Spring's caching infrastructure...

@Configuration
@EnableCaching
class MyCachingConfiguration {

  @Bean
  CustomCacheManager cacheManager() {
    return new CustomCacheManager();
  }
}

That's it; you are all set!

Anyway, I hope this helps give you an idea of what you must do.

Cheers! -John

Upvotes: 2

Sundararaj Govindasamy
Sundararaj Govindasamy

Reputation: 8495

From Spring docs,

Custom cache resolution: The default cache resolution fits well for applications working with a single CacheManager and with no complex cache resolution requirements.

For applications working with several cache managers, it is possible to set the cacheManager to use per operation:

Refer spring doc here

Upvotes: 0

Related Questions