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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35917159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:15:27  来源:igfitidea点击:

Spring Boot - How to disable @Cacheable during development?

javaspringspring-bootspring-elspring-cache

提问by Wouter

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){
       ...
    }
    
  1. 如何使用 Spring boot“dev”配置文件在开发过程中禁用所有缓存。在 application.properties 中没有任何通用设置可以将其全部关闭。最简单的方法是什么?

  2. 如何禁用特定方法的缓存?我尝试像这样使用 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.

我不想让事情过于复杂。

采纳答案by M. Deinum

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

默认情况下会自动检测和配置缓存类型。但是,您可以通过添加spring.cache.type到配置中来指定要使用的缓存类型。要禁用它,请将值设置为NONE

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

当您想为特定配置文件执行此操作时,将其添加到该配置文件application.properties中,在这种情况下修改application-dev.properties并添加

spring.cache.type=NONE

This will disable caching.

这将禁用缓存。

回答by 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

然后将其用于可缓存注释上的拼写条件

回答by davidxxx

The David Newcomb commenttells the truth :

大卫·纽科姆评论道出真相:

spring.cache.type=NONEdoesn'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".

spring.cache.type=NONE不会关闭缓存,它会阻止缓存内容。即它仍然为您的程序添加了 27 层 AOP/拦截器堆栈,只是它不进行缓存。这取决于他所说的“全部关闭”是什么意思。

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 @EnableCachingclass in a dedicated configuration class that we will wrap with a @Profileto enable it :

@EnableCaching类移动到专用配置类中,我们将用 a 包装@Profile它以启用它:

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

Of course if you already have a Configurationclass that is enabled for all but the devenvironment, just reuse it :

当然,如果您已经有一个Configuration为除dev环境之外的所有对象启用的类,只需重用它:

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

2) Use a fake (noop) Cache manager

2)使用假(noop)缓存管理器

In some cases, activating @EnableCachingby 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.CacheManagerinterface.
In this case, the right way is using a fake implementation that will allow Spring to resolve all dependencies while the implementation of the CacheManageris overhead free.

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

We could achieve it by playing with @Beanand @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=NONEproperty that produces the same result as written in the M. Deinum answer.

或者,如果它更合适,您可以添加spring.cache.type=NONE产生与 M. Deinum 答案中所写结果相同的属性。