Ist es möglich, Spring -Boot -Cache mit Koffein über eine Eigenschaft zu aktivieren/zu deaktivieren?
Posted: 11 Feb 2025, 23:32
Ich möchte Springboot -Cache mit Koffein -Cache aktivieren/deaktivieren. .cache.enabled = false zum Aktivieren und Deaktivieren von Caching. Dieser Code: < /p>
(falls dies existiert) oder meine eigene Eigenschaft, um das Caching zu aktivieren/zu deaktivieren?
Code: Select all
@Configuration
@EnableCaching
class ReportCacheConfiguration {
@Bean
CacheManagerCustomizer cacheManagerCustomizer(MeterRegistry registry) {
return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
}
private Cache reportCache() {
return Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build()
;
}
}
< /code>
@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(...);
}
}
< /code>
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 cacheManagerCustomizer(MeterRegistry registry) {
// return cacheManager -> cacheManager.registerCustomCache("report", reportCache());
// }
//
// private Cache reportCache() {
// return Caffeine.newBuilder()
// .expireAfterWrite(1, TimeUnit.MINUTES)
// .build()
// ;
// }
//
//}
< /code>
This is kinda "dumb" in my opinion.
Is it possible, with some spring configuration, something like springboot.cache.enabled=true