Guache Cache CacheStats all zero

I am using Guava Cache lib and I want to check cache statistics here, this 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 called recordeStats()

to activate stats, but when I print stats, everything is 0.enter image description here

+3


source to share


4 answers


According to JavaDoc:

The cache statistics are increased according to the following rules:



  • When a cache lookup encounters an existing cache entry, hitCount is incremented.
  • When a cache lookup first encounters a missing cache entry, a new entry is loaded.
  • Upon successful loading, missCount and loadSuccessCount entries are incremented and the total load time in nanoseconds is added to the totalLoadTime.
  • When an exception is thrown while loading a record, missCount and loadExceptionCount are incremented, and the total load time in nanoseconds is added to the totalLoadTime.
  • Finding caches that encounter a missing cache entry that is still loading will wait for the download to complete (whether successful or not) and then increment missCount.
  • When the entry is out of the cache, evictionCount is incremented.
  • No statistics are changed if a cache entry is invalid or manually deleted.
  • None of the statistics are changed by operations called in the asMap view of the cache.

Search is defined specifically as calling one of the LoadCache.get (Object), LoadingCache.getUnchecked (Object), Cache.get (Object, Callable), or LoadingCache.getAll (Iterable) methods.

+6


source


You forgot to activate the cache entry by adding recordStats()

to CacheBuilder.newBuilder()

.



+23


source


In your code, you are not actually using the cache before getting its stats ... why is it surprising that the stats are 0?

+2


source


You cannot create statistics (). It is a factory that copies the cache values ​​into the final statistics. So at runtime use logger.info (refreshCache.stats ()).

+1


source







All Articles