Adams.H
Adams.H

Reputation: 1653

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

Upvotes: 10

Views: 6211

Answers (4)

Roland Ettinger
Roland Ettinger

Reputation: 2835

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

Upvotes: 44

Thomas
Thomas

Reputation: 1732

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.

Upvotes: 2

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

According to JavaDoc:

Cache statistics are incremented 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.
  • After successfully loading an entry missCount and loadSuccessCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
  • When an exception is thrown while loading an entry, missCount and loadExceptionCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
  • Cache lookups that encounter a missing cache entry that is still loading will wait for loading to complete (whether successful or not) and then increment missCount.--
  • When an entry is evicted from the cache, evictionCount is incremented.
  • No stats are modified when a cache entry is invalidated or manually removed.
  • No stats are modified by operations invoked on the asMap view of the cache.

A lookup is specifically defined as an invocation of one of the methods LoadingCache.get(Object), LoadingCache.getUnchecked(Object), Cache.get(Object, Callable), or LoadingCache.getAll(Iterable).

Upvotes: 7

ColinD
ColinD

Reputation: 110046

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

Upvotes: 2

Related Questions