Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration for specific Caffeine Caches in Spring

We need to implement several methods that have different caching times. Each method is annotated with @Cacheable and our current solution includes multiple CacheManager that are set in a CachingConfigurerSupport.

public class CachingConfiguration extends CachingConfigurerSupport {

  @Override
  @Bean
  public CacheManager cacheManager() {
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.DAYS));
    return cacheManager;
  }

  @Bean
  public CacheManager anotherCache() {
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES));
    return cacheManager;
  }
}

The @Cacheable annotation then included the cacheManager: @Cacheable(cacheNames = "someCache", cacheManager = "anotherCache")

Basically that's fine but is also errorprune if you forget the cacheManager parameter etc.

So I currently try to find a better solution but as far as I can see, there is currently no general accepted way to go.

Imho the main advantage of the CaffeineCacheManager compared to e.g. SimpleCacheManager is the possibility to define a base configuration and then initialize additional caches lazily. But wouldn't it be great if you are able to set additional caches which are never reinitialized?

Those caches must be used preferentially and created in the CachingConfigurerSupport.

Maybe I'm missing something, but shouldn't this solve the problem that has already been discussed in several threads in different forms?

like image 956
deveth0 Avatar asked Oct 31 '25 18:10

deveth0


1 Answers

Recently I decided to turn my initial PR into a separate tiny project.

To start using it just add the latest dependency from Maven Central:

<dependency>
    <groupId>io.github.stepio.coffee-boots</groupId>
    <artifactId>coffee-boots</artifactId>
    <version>2.0.0</version>
</dependency>

Format of properties is the following:

coffee-boots.cache.spec.myCache=maximumSize=100000,expireAfterWrite=1m

If no specific configuration is defined, CacheManager defaults to Spring's behavior.

like image 69
stepio Avatar answered Nov 04 '25 16:11

stepio