Spring Boot - How to disable @Cachable during development?

二次信任 提交于 2019-11-28 23:19:47

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.

PaulNUK

For your second question do something like this:

Write a method that determines whether or not a particular profile is active (environment is your injected Environment)

boolean isProfileActive(String profile) { 
   return Arrays.asList(environment.getActiveProfiles()).contains(profile);
}

then use that for your spel condition on the cacheable annotation

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 will not fast up the application startup and the overhead of the cache manager feature. It will simply not caching objects.

Besides your cache configuration can specify an important max Heap parameter (such as maxBytesLocalHeap"4000m" in EhCache) that will constraint you to allocate enough memory as soon as the Spring Boot startup. A real waste if you don't use cache.

So if what you want is speeding up your spring boot startup in dev and reducing the cpu/memory overhead of the running program, you have to disable the Spring cache feature or as alternative using a faked cache manager (and Spring provides that).
Both ways should produce a very close result.

1) Disable the Spring Cache management

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!