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

dfddblmv  于 2023-10-14  发布在  Java
关注(0)|答案(2)|浏览(152)

我需要在我的服务中使用几个缓存用于不同的用途。我正在寻找一种方法来分离它们的配置,如maximumSizeMaximreAfterWrite。我使用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,它将具有不同的配置。
我怎么能这么做呢?谢谢你,谢谢

vbopmzt1

vbopmzt11#

您可以通过编程方式声明许多缓存配置,而不是使用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<>();
       
       // 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();
    }
    
}
2vuwiymt

2vuwiymt2#

最适合您的解决方案。

@Configuration
@EnableCaching
public class CacheConfig {
    @Value("${cache.cache-names.otpCache}")
    String otpCacheName;
    @Value("${cache.cache-names.tokenCache}")
    String tokenCacheName;
    @Value("${cache.ttl.otpCache}")
    int otpCacheTTL;
    @Value("${cache.ttl.tokenCache}")
    int tokenCacheTTL;

    @Bean("otp-cache")
    public CacheManager tokenCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager(otpCacheName);
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .expireAfterWrite(otpCacheTTL, TimeUnit.SECONDS));
        return cacheManager;
    }

    @Bean("token-cache")
    public CacheManager otpCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager(tokenCacheName);
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .expireAfterWrite(tokenCacheTTL, TimeUnit.HOURS));
        return cacheManager;
    }
}

相关问题