'Unable to get stats for Caffeine cache (Spring 5)

I got stuck on a (hopefully) very simple problem. I added a Caffeine cache to my Spring application and this cache works well. Now I tried to get the statistics for this cache, but I can't find a way to get to the right methods...

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CachingConfig {

    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager("bin");
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(1, TimeUnit.DAYS)
                .recordStats());
        return cacheManager;
    }

}

How can I get stats for this cache?



Solution 1:[1]

Inject CacheManager and then just get it.

CacheStats stats = ((CaffeineCache) cacheManager.getCache("bin")).getNativeCache().stats();

Works for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafa?.x.w