Define multiple caches configurations with Spring and Caffeine
Asked Answered
B

2

3

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!

Broccoli answered 31/8, 2020 at 11:56 Comment(1)
The Spring authors were against that feature, but an alternative adapter offers that functionality.Olindaolinde
A
5

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();
    }
    
}
Achernar answered 2/9, 2020 at 0:41 Comment(4)
How to find the cache size for cacheBuilder.maximumSize(maxSize);. I am here trying to cache for 20 min. Is the size is in bite or byte or KB or GB or what?Overlook
@SathishKumarkk the maximum cache size is in number of entries.Achernar
In my case I am going to case the method that returns a JWT token. So shall I have size as 1(one)? Since I am going to have only one entry and will update it after 20 min.Overlook
@SathishKumarkk the maximumSize is a limit, is useful when you want to avoid the cache to grow to a dangerous size (in terms of available memory). But if you don't expect the number of entries to grow, you can skip setting a limit. Think if that JWT is one per application, or one per cache key (user, etc), and that will give you the expected number of entries.Achernar
D
1

Best solution for you.

@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;
    }
}
Duffey answered 9/10, 2023 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.