<p>I need to use several caches in my service for different uses.
I'm looking for a way to separate their configurations like <strong>maximumSize</strong> and <strong>expireAfterWrite</strong>.
I'm using Spring and Kubernetes, and in the deploy.yaml I have this:</p>
<pre><code>spring:
  main:
    allow-bean-definition-overriding: true
  cache:
    type: CAFFEINE
    cache-names: cacheA, cacheB
    caffeine:
      spec: expireAfterWrite=1h,maximumSize=2000
  output:
    ansi:
      enabled: never
</code></pre>
<p>I want to create a new cache, <strong>cacheC</strong>, which will have a different configurations.</p>
<p>How can I do that? Thanks!</p>

<p>You can declare many cache configurations programmatically instead of using yaml.</p>
<p>Something like this:</p>
<pre><code>@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();
    }
    
}
</code></pre>
