【问题标题】:Spring Boot - How to disable @Cacheable for spring.profiles.active = test?Spring Boot - 如何为 spring.profiles.active = test 禁用 @Cacheable?
【发布时间】:2021-01-09 19:53:13
【问题描述】:

我需要为 spring.profiles.active = test 的方法启用 in_memory 缓存。只需要缓存最近 10 分钟的请求。

现在我这样设置:

@Profile("test")
@Configuration
class CachingConfig {
    @Bean
    fun cacheManager() = Caffeine.newBuilder()
        .expireAfterWrite(10, TimeUnit.MINUTES)
        .build<Any, Any>()
}
@Cacheable("request", key = "#dto.id")
fun request(dto: RequestDTO): Any {
    ...
}
@EnableCaching
@SpringBootApplication
@EnableConfigurationProperties
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

build.gragle

test {
    systemProperty "spring.profiles.active", "test"
}

但我不敢扩展它,因为我不确定它是否能在那里正常工作。

我不完全明白:这对 spring.profiles.active = prod 有什么作用?

  1. cacheManager bean 未引发时是否会出错 产品?
  2. 会不会出现在 prod spring 上默认提升 cacheManager 的情况?

【问题讨论】:

    标签: spring-boot kotlin environment-variables spring-cache spring-profiles


    【解决方案1】:

    您需要将 @EnableCachingclass Application 移动到 class CachingConfig 以使缓存仅适用于 Profile = "test"

    @Profile("test")
    @EnableCaching
    @Configuration
    class CachingConfig { ... }
    

    使用这种方法,当非“测试”配置文件处于活动状态时,CachingConfig 甚至不会被加载到应用程序上下文中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 2022-07-13
      • 2021-11-09
      • 2018-02-07
      • 2020-12-29
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      相关资源
      最近更新 更多