使用Spring和Caffeine定义多个缓存配置。

huangapple go评论70阅读模式
英文:

Define multiple caches configurations with Spring and Caffeine

问题

我需要在我的服务中使用几个不同用途的缓存。
我正在寻找一种将它们的配置(如maximumSizeexpireAfterWrite)分开的方法。
我正在使用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 = &quot;cacheA&quot;;
public static final String CACHE_B = &quot;cacheB&quot;;
public static final String CACHE_C = &quot;cacheC&quot;;
@Bean
public CacheManager cacheManagerTicker(Ticker ticker) {
List&lt;Cache&gt; caches = new ArrayList&lt;&gt;();
// 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&lt;Object, Object&gt; cacheBuilder = Caffeine.newBuilder();
// TTL
if (ttl != null &amp;&amp; ttl &gt; 0 &amp;&amp; ttlUnit != null){
cacheBuilder.expireAfterWrite(ttl, ttlUnit);
}
// Max size
if (maxSize != null &amp;&amp; maxSize &gt; 0){
cacheBuilder.maximumSize(maxSize);
}
// Ticker
cacheBuilder.ticker(ticker);
return new CaffeineCache(cacheName, cacheBuilder.build());
}
@Bean
public Ticker ticker() {
return Ticker.systemTicker();
}
}

huangapple
  • 本文由 发表于 2020年8月31日 19:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定