javaspring-bootcachingcaffeine

Caffeine Springboot integration


we are using caffeine to replace the ConcurrentHashMap cache, which is default in current springboot. We are dynamically creating caches using the @Cacheable(cacheNames = { "..." }) annotation.

I am trying to set the recordStats property, since we are using the springboot actuator package to monitor various aspects of our applications.

I tried to set spring.cache.caffeine.spec=expireAfterAccess=3600s,recordStats in application.properties, which does not work.

Setting it in a @Configure class did not work either:

@Configuration
public class CacheConfig {

  @Bean
  public CacheManager cacheManager() {
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    cacheManager.setCacheSpecification("expireAfterAccess=3600s,recordStats");
    return cacheManager;
  }
}

The cache statistics does not appear in the /actuator/cache/{caches} endpoint or in our springboot-admin server.

Taking from the current api documentation, i found:

The string syntax is a series of comma-separated keys or key-value pairs, each corresponding to a Caffeine builder method.

initialCapacity=[integer]: sets Caffeine.initialCapacity.

...

recordStats: sets Caffeine.recordStats(). 

Durations are represented by an integer, followed by one of "d", "h", "m", or "s", representing days, hours, minutes, or seconds respectively. There is currently no syntax to request expiration in milliseconds, microseconds, or nanoseconds.

Whitespace before and after commas and equal signs is ignored. Keys may not be repeated; it is also illegal to use the following pairs of keys in a single value:

maximumSize and maximumWeight
weakValues and softValues 

And the relevant point:

CaffeineSpec does not support configuring Caffeine methods with non-value parameters. These must be configured in code.

Is there no possibility to achieve my task?

thanks


Solution

  • I managed to get it work. Here is the code:

    @Configuration
    public class CacheConfig {
    
      @Bean
      public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager("cache1",
            "cache2", "cache3");
        cacheManager.setCacheSpecification("recordStats");
        return cacheManager;
      }
    }
    

    still with the downside, that the cache names must match those in the @Cacheable(cachenames={"..."}) annotation