简体   繁体   English

Spring Boot - 如何在开发过程中禁用 @Cacheable?

[英]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.如何使用 Spring boot“dev”配置文件在开发过程中禁用所有缓存。 There doesn't seam to be a general setting to turn it all off in application.properties.在 application.properties 中没有任何通用设置可以将其全部关闭。 What's the easiest way?最简单的方法是什么?

  2. How to disable caching for a specific method?如何禁用特定方法的缓存? I tried to use SpEl like this:我尝试像这样使用 SpEl:

     @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.关于 SO 有几个与此相关的问题,但它们指的是 XML 配置或其他内容,但我使用的是 Spring Boot 1.3.3 并且它使用自动配置。

I don't want to over-complicate things.我不想让事情过于复杂。

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.但是,您可以通过将spring.cache.type添加到您的配置中来指定要使用的缓存类型。 To disable it set the value to NONE .要禁用它,请将值设置为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当您想为特定配置文件执行此操作时,将其添加到该配置文件application.properties在这种情况下修改application-dev.properties并添加

spring.cache.type=NONE

This will disable caching.这将禁用缓存。

The David Newcomb comment tells the truth : 大卫纽科姆的评论说的是实话:

spring.cache.type=NONE doesn't switch caching off, it prevents things from being cached. spring.cache.type=NONE不会关闭缓存,它会阻止缓存内容。 ie it still adds 27 layers of AOP/interceptor stack to your program, it's just that it doesn't do the caching.即它仍然为您的程序添加了 27 层 AOP/拦截器堆栈,只是它不进行缓存。 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 1)完全禁用Spring Cache功能

Move the @EnableCaching class in a dedicated configuration class that we will wrap with a @Profile to enable it :@EnableCaching类移动到专用配置类中,我们将用@Profile包装该类以启用它:

@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 :当然,如果您已经有一个为除dev环境之外的所有环境启用的Configuration类,只需重用它:

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

2) Use a fake (noop) Cache manager 2)使用假(noop)缓存管理器

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.在某些情况下,通过配置文件激活@EnableCaching是不够的,因为您的某些类或应用程序的某些 Spring 依赖项希望从 Spring 容器中检索实现org.springframework.cache.CacheManager接口的 bean。
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.在这种情况下,正确的方法是使用一个假实现,它允许 Spring 解决所有依赖项,而CacheManager的实现是免费的。

We could achieve it by playing with @Bean and @Profile :我们可以通过使用@Bean@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.或者,如果它更合适,您可以添加spring.cache.type=NONE属性,该属性产生与 M. Deinum 答案中写入的结果相同的结果。

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然后将其用于可缓存注释上的拼写条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM