Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - How to disable @Cacheable during development?

I'm looking for 2 things:

  1. How to disable all caching during development with Spring boot "dev" profile. There doesn't seam to be a general setting to turn it all off in application.properties. What's the easiest way?

  2. How to disable caching for a specific method? I tried to use SpEl like this:

    @Cacheable(value = "complex-calc", condition="#${spring.profiles.active} != 'dev'}") public String someBigCalculation(String input){    ... } 

But I can get it to work. There are a couple of questions on SO related to this, but they refer to XML config or other things, but I'm using Spring Boot 1.3.3 and this uses auto-configuration.

I don't want to over-complicate things.

like image 225
Wouter Avatar asked Mar 10 '16 12:03

Wouter


People also ask

Which is the spring boot annotation used for caching auto configuration?

Spring Boot auto-configures the cache infrastructure as long as caching support is enabled via the @EnableCaching annotation.

What is the use of @cacheable?

As the name implies, @Cacheable is used to demarcate methods that are cacheable - that is, methods for whom the result is stored into the cache so on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually execute the method.


2 Answers

The type of cache is by default automatically detected and configured. However you can specify which cache type to use by adding spring.cache.type to your configuration. To disable it set the value to NONE.

As you want to do it for a specific profile add it to that profiles application.properties in this case modify the application-dev.properties and add

spring.cache.type=NONE 

This will disable caching.

like image 185
M. Deinum Avatar answered Sep 17 '22 15:09

M. Deinum


The David Newcomb comment tells the truth :

spring.cache.type=NONE doesn't switch caching off, it prevents things from being cached. i.e. it still adds 27 layers of AOP/interceptor stack to your program, it's just that it doesn't do the caching. It depends what he means by "turn it all off".

Using this option may fast up the application startup but could also have some overheads.

1)To disable completely the Spring Cache feature

Move the @EnableCaching class in a dedicated configuration class that we will wrap with a @Profile to enable it :

@Profile("!dev") @EnableCaching @Configuration public class CachingConfiguration {} 

Of course if you already have a Configuration class that is enabled for all but the dev environment, just reuse it :

@Profile("!dev") //... any other annotation  @EnableCaching @Configuration public class NoDevConfiguration {} 

2) Use a fake (noop) Cache manager

In some cases, activating @EnableCaching by profile is not enough because some of your classes or some Spring dependencies of your app expect to retrieve from the Spring container a bean implementing the org.springframework.cache.CacheManager interface.
In this case, the right way is using a fake implementation that will allow Spring to resolve all dependencies while the implementation of the CacheManager is overhead free.

We could achieve it by playing with @Bean and @Profile :

import org.springframework.cache.support.NoOpCacheManager;   @Configuration public class CacheManagerConfiguration {      @Bean     @Profile("!dev")     public CacheManager getRealCacheManager() {         return new CaffeineCacheManager();          // or any other implementation         // return new EhCacheCacheManager();      }      @Bean     @Profile("dev")     public CacheManager getNoOpCacheManager() {         return new NoOpCacheManager();     } } 

Or if it is more suitable, you can add the spring.cache.type=NONE property that produces the same result as written in the M. Deinum answer.

like image 32
davidxxx Avatar answered Sep 19 '22 15:09

davidxxx