Spring Boot-如何在开发过程中禁用@Cacheable? [英] Spring Boot - How to disable @Cacheable during development?

查看:1221
本文介绍了Spring Boot-如何在开发过程中禁用@Cacheable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找2件东西:

  1. 如何在使用Spring boot"dev"配置文件进行开发期间禁用所有缓存.没有接缝可以在application.properties中将其全部关闭.最简单的方法是什么?

  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?

如何为特定方法禁用缓存?我试图像这样使用SpEl:

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){
   ...
}

但是我可以使它工作.关于SO,有几个与此相关的问题,但是它们涉及XML配置或其他内容,但是我使用的是Spring Boot 1.3.3,并且使用了自动配置.

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.

我不想使事情复杂化.

推荐答案

spring.cache.type=NONE不会关闭缓存,它可以防止出现问题 从被缓存.即它仍然增加了27层AOP/拦截器 堆栈到您的程序,只是它不进行缓存.它 取决于他所说的全部关闭".

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)要完全禁用Spring Cache功能

@EnableCaching类移动到专用的配置类中,我们将在其中将其包装为@Profile来启用它:

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 {}

当然,如果您已经拥有除dev环境之外的所有环境都已启用的Configuration类,则只需重用它即可:

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)使用伪造的(临时)缓存管理器

在某些情况下,通过概要文件激活@EnableCaching是不够的,因为某些类或应用程序的某些Spring依赖项希望从Spring容器中检索实现org.springframework.cache.CacheManager接口的bean.
在这种情况下,正确的方法是使用伪造的实现,该实现将允许Spring解析所有依赖项,而CacheManager的实现是无开销的.

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.

我们可以通过玩@Bean@Profile来实现它:

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();
    }
}

或更合适的话,您可以添加spring.cache.type=NONE属性,该属性产生与M. Deinum答案中相同的结果.

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 Boot-如何在开发过程中禁用@Cacheable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆