英文:
Define multiple caches configurations with Spring and Caffeine
问题
我需要在我的服务中使用几个不同用途的缓存。
我正在寻找一种将它们的配置(如maximumSize和expireAfterWrite)分开的方法。
我正在使用Spring和Kubernetes,在deploy.yaml中我有以下配置:
spring:
main:
allow-bean-definition-overriding: true
cache:
type: CAFFEINE
cache-names: cacheA, cacheB
caffeine:
spec: expireAfterWrite=1h, maximumSize=2000
output:
ansi:
enabled: never
我想要创建一个名为cacheC的新缓存,它将具有不同的配置。
我该如何做?谢谢!
英文:
I need to use several caches in my service for different uses.
I'm looking for a way to separate their configurations like maximumSize and expireAfterWrite.
I'm using Spring and Kubernetes, and in the deploy.yaml I have this:
spring:
main:
allow-bean-definition-overriding: true
cache:
type: CAFFEINE
cache-names: cacheA, cacheB
caffeine:
spec: expireAfterWrite=1h,maximumSize=2000
output:
ansi:
enabled: never
I want to create a new cache, cacheC, which will have a different configurations.
How can I do that? Thanks!
答案1
得分: 4
你可以以编程方式声明许多缓存配置,而不是使用yaml。
类似这样:
@Configuration
@EnableCaching
public class CacheConfig {
public static final String CACHE_A = "cacheA";
public static final String CACHE_B = "cacheB";
public static final String CACHE_C = "cacheC";
@Bean
public CacheManager cacheManagerTicker(Ticker ticker) {
List<Cache> caches = new ArrayList<>();
// 缓存 A
caches.add(this.buildCache(CACHE_A, ticker, 2000L, 1L, TimeUnit.HOURS));
// 缓存 B
caches.add(this.buildCache(CACHE_B, ticker, 2000L, 1L, TimeUnit.HOURS));
// 缓存 C
caches.add(this.buildCache(CACHE_C, ticker, 3500L, 15L, TimeUnit.MINUTES));
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return cacheManager;
}
private CaffeineCache buildCache(String cacheName, Ticker ticker, Long maxSize, Long ttl, TimeUnit ttlUnit){
Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
// 生存时间
if (ttl != null && ttl > 0 && ttlUnit != null){
cacheBuilder.expireAfterWrite(ttl, ttlUnit);
}
// 最大尺寸
if (maxSize != null && maxSize > 0){
cacheBuilder.maximumSize(maxSize);
}
// 计时器
cacheBuilder.ticker(ticker);
return new CaffeineCache(cacheName, cacheBuilder.build());
}
@Bean
public Ticker ticker() {
return Ticker.systemTicker();
}
}
英文:
You can declare many cache configurations programmatically instead of using yaml.
Something like this:
@Configuration
@EnableCaching
public class CacheConfig {
public static final String CACHE_A = "cacheA";
public static final String CACHE_B = "cacheB";
public static final String CACHE_C = "cacheC";
@Bean
public CacheManager cacheManagerTicker(Ticker ticker) {
List<Cache> caches = new ArrayList<>();
// Cache A
caches.add(this.buildCache(CACHE_A, ticker, 2000L, 1L, TimeUnit.HOURS));
// Cache B
caches.add(this.buildCache(CACHE_B, ticker, 2000L, 1L, TimeUnit.HOURS));
// Cache C
caches.add(this.buildCache(CACHE_C, ticker, 3500L, 15L, TimeUnit.MINUTES));
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return cacheManager;
}
private CaffeineCache buildCache(String cacheName, Ticker ticker, Long maxSize, Long ttl, TimeUnit ttlUnit){
Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
// TTL
if (ttl != null && ttl > 0 && ttlUnit != null){
cacheBuilder.expireAfterWrite(ttl, ttlUnit);
}
// Max size
if (maxSize != null && maxSize > 0){
cacheBuilder.maximumSize(maxSize);
}
// Ticker
cacheBuilder.ticker(ticker);
return new CaffeineCache(cacheName, cacheBuilder.build());
}
@Bean
public Ticker ticker() {
return Ticker.systemTicker();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论