admin管理员组文章数量:1300009
I would like to enable/disable springboot cache with caffeine cache.
With a property, something like my.property.cache.enabled=true
or my.property.cache.enabled=false
to enable and disable caching.
This is not about runtime enabling/disabling cache.
For instance this code:
@Configuration
@EnableCaching
class ReportCacheConfiguration {
@Bean
CacheManagerCustomizer<CaffeineCacheManager> cacheManagerCustomizer(MeterRegistry registry) {
return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
}
private Cache<Object, Object> reportCache() {
return Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build()
;
}
}
@Service
public class BookServiceImpl implements BookService {
@Cacheable(cacheNames = "report")
public Book findBook(String isbn) {
//some very expensive network call
//then some very expensive database call
return new Book(...);
}
}
With the code above, especially when the third party network call, or the database is overloaded, the caching is very helpful.
However, there are moments where the third party service or the database is under load, in which case, we want to actually "hit the target and do the expensive thing".
To do so, we currently comment the code, create a new build and redeploy...
@Service
public class BookServiceImpl implements BookService {
//@Cacheable(cacheNames = "report")
public Book findBook(String isbn) {
//some very expensive network call
//then some very expensive database call
return new Book(...);
}
}
//@Configuration
//@EnableCaching
//class ReportCacheConfiguration {
//
// @Bean
// CacheManagerCustomizer<CaffeineCacheManager> cacheManagerCustomizer(MeterRegistry registry) {
// return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
// }
//
// private Cache<Object, Object> reportCache() {
// return Caffeine.newBuilder()
// .expireAfterWrite(1, TimeUnit.MINUTES)
// .build()
// ;
// }
//
//}
This is kinda "dumb" in my opinion.
Is it possible, with some spring configuration, something like springboot.cache.enabled=true
(if that exists) or my own custom property to enable/disable caching?
I would like to enable/disable springboot cache with caffeine cache.
With a property, something like my.property.cache.enabled=true
or my.property.cache.enabled=false
to enable and disable caching.
This is not about runtime enabling/disabling cache.
For instance this code:
@Configuration
@EnableCaching
class ReportCacheConfiguration {
@Bean
CacheManagerCustomizer<CaffeineCacheManager> cacheManagerCustomizer(MeterRegistry registry) {
return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
}
private Cache<Object, Object> reportCache() {
return Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build()
;
}
}
@Service
public class BookServiceImpl implements BookService {
@Cacheable(cacheNames = "report")
public Book findBook(String isbn) {
//some very expensive network call
//then some very expensive database call
return new Book(...);
}
}
With the code above, especially when the third party network call, or the database is overloaded, the caching is very helpful.
However, there are moments where the third party service or the database is under load, in which case, we want to actually "hit the target and do the expensive thing".
To do so, we currently comment the code, create a new build and redeploy...
@Service
public class BookServiceImpl implements BookService {
//@Cacheable(cacheNames = "report")
public Book findBook(String isbn) {
//some very expensive network call
//then some very expensive database call
return new Book(...);
}
}
//@Configuration
//@EnableCaching
//class ReportCacheConfiguration {
//
// @Bean
// CacheManagerCustomizer<CaffeineCacheManager> cacheManagerCustomizer(MeterRegistry registry) {
// return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
// }
//
// private Cache<Object, Object> reportCache() {
// return Caffeine.newBuilder()
// .expireAfterWrite(1, TimeUnit.MINUTES)
// .build()
// ;
// }
//
//}
This is kinda "dumb" in my opinion.
Is it possible, with some spring configuration, something like springboot.cache.enabled=true
(if that exists) or my own custom property to enable/disable caching?
1 Answer
Reset to default 1You can use @ConditionalOnProperty
annotation on your configuration class
@Configuration
@EnableCaching
@ConditionalOnProperty("my.property.cache.enabled")
class ReportCacheConfiguration {
...
}
All configuration file will be not enabled if my.property.cache.enabled
will be not true
本文标签: javaIs it possible to enabledisable springboot cache with caffeine via a propertyStack Overflow
版权声明:本文标题:java - Is it possible to enabledisable springboot cache with caffeine via a property? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741630893a2389357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论