Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava Cache CacheStats all zero

I'm using Guava Cache lib ,and i wanna check the statistics of the cache here is my code :

refreshCache = CacheBuilder.newBuilder()
           .refreshAfterWrite(1, TimeUnit.MINUTES)
           .maximumSize(5)
           .recordStats()
           .build(
               new CacheLoader<String, Map<String,Object>>() {
                 public Map<String,Object> load(String key) throws Exception {
                     loader();
                 }
               });
        stats = refreshCache.stats();
        delta = refreshCache.stats()
                .minus(stats);

}

 logger.info("Stats:{}",stats);
 logger.info("delta:{}",delta);

i've called recordeStats() to active the Stats , but when i print the stats , it's all 0 .enter image description here

like image 215
Adams.H Avatar asked Aug 22 '14 08:08

Adams.H


2 Answers

You forgot to activate the cache recording by adding recordStats() to CacheBuilder.newBuilder().

like image 95
Roland Ettinger Avatar answered Sep 28 '22 03:09

Roland Ettinger


You can not instantiate stats(). It is a factory which copies over the cache values to a final statistic. So please use logger.info(refreshCache.stats()) during runtime.

like image 44
Thomas Avatar answered Sep 28 '22 03:09

Thomas