dexdeb
dexdeb

Reputation: 63

Enable/Disable Caching for specific methods in a service based on configuration

We are using Spring's Caching abstraction to add caching behaviour to our services.

By default, @Enablecaching annotation either enables/disables caching for entire service.

The @Cacheable annotation has been used on all methods.

But, now we want to enable caching for some endpoints/methods and disable caching for other endpoints

Is there a way to achieve that with removing the added annotations in the service something like an Interceptor/Filter vetoing the caching behaviour for certain methods based on configuration.

Upvotes: 0

Views: 2130

Answers (1)

Aamir Latif
Aamir Latif

Reputation: 56

One work around for this You can make your method conditional for cache on the base of parameter. The cache annotations support such functionality through the condition parameter which takes a SpEL expression that is evaluated to either true or false.

@Cacheable(cacheNames="book", condition="#cached == false")
public Book findBook(boolean cached)

As explained in documentation. https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html

Upvotes: 0

Related Questions