Enable/Disable Caching for specific methods in a s

2019-09-16 16:08发布

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.

1条回答
Juvenile、少年°
2楼-- · 2019-09-16 16:36

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

查看更多
登录 后发表回答