Original address: Guava Library Learning: Learning Guava Cache (vi) cachestats
Previous article,Guava Library Learning: Learning Guava Cache (v) cacheloader , we learned Cacheloader abstract class, mainly from the method, To receive a function or supplier and return a Cacheloader instance, we have learned how to create a powerful caching mechanism, and then we want to collect some statistics after the cache is executed or used. Now start this article, Guava Cache cachestats learning.
The guava cache provides a very simple way to collect statistics on cache execution, and it is important to note that the tracking cache operation will result in a performance penalty, and to collect cached information, we only need to declare when we use cachebuilder that we want to collect statistics:
loadingcache<string,tradeaccount> Tradeaccountcache = Cachebuilder.newbuilder (). RecordStats ()
The above code, we construct a Loadingcache instance through the builder pattern, want to enable statistics of the cache information, the only thing we need to do is to build inside the builder.recordstats ()register, and want to get statistical information, we only need to call the stats () method through the cache or Loadingcache, we will return a cachestats instance, through the Cachestats instance can obtain the required statistics, see the following example:
Cachestats cachestats = Cache.stats ();
Here is an overview of a list of some of the information we can get through Cachestats:
The average time spent loading cached entry values;
The hit rate of the requested cache entry;
The misses of the requested cache entry;
The number of cache bars removed;
There is a lot of information involved in cache performance, the above list is just some of the information we get through cachestats, finally, we open the source of cachestats, to collate the public methods provided, as follows:
RequestCount (): Returns the cache's Lookup method to find the number of caches, regardless of whether the lookup value is cached.
HitCount (): Returns the number of cache hits cached by the lookup method of the cache.
Hitrate (): Returns the hit rate of the cache request, divided by the number of requests.
Misscount (): Returns the number of misses for the cache request.
Missrate (): Returns the ratio of cache request misses, the number of misses divided by the number of requests.
Loadcount (): Returns the number of times the cache calls the Load method to load a new value.
Loadsuccesscount (): Returns the number of times the cache has succeeded in loading a new value.
Loadexceptioncount (): Returns the number of times the cache loaded a new value with an exception.
Loadexceptionrate (): Returns the rate at which the cache loaded the new value with an exception.
Totalloadtime (): Returns the total time spent by the cache to load new values.
Averageloadpenalty (): The average time that the cache loads new values, the number of loads divided by the total time loaded.
Evictioncount (): Returns the number of times the entry in the cache has been removed.
Minus (Cachestats Other): Returns a new Cachestats instance that represents the difference between the current cachestats and the incoming cachestats.
Plus (Cachestats other): Returns a new Cachestats instance representing the total between the current cachestats and the incoming cachestats.
Guava Library Learning: Learning Guava Cache (vi) cachestats