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屋!