Class CacheStats
Cache.
Cache statistics are incremented according to the following rules:
- When a cache lookup encounters an existing cache entry
hitCountis incremented. - When a cache lookup first encounters a missing cache entry, a new entry is loaded.
- After successfully loading an entry
missCountandloadSuccessCountare incremented, and the total loading time, in nanoseconds, is added tototalLoadTime. - When an exception is thrown while loading an entry or if the loaded value is {code null},
missCountandloadFailureCountare incremented, and the total loading time, in nanoseconds, is added tototalLoadTime. - 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.
- After successfully loading an entry
- When an entry is computed through the asMap the
loadSuccessCountorloadFailureCountis incremented. - When an entry is evicted from the cache,
evictionCountis incremented and the weight added toevictionWeight. - No stats are modified when a cache entry is invalidated or manually removed.
- No stats are modified by non-computing 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), Cache.get(Object, java.util.function.Function), or
LoadingCache.getAll(Iterable).
This is a value-based class; use of identity-sensitive operations (including reference
equality (==), identity hash code, or synchronization) on instances of CacheStats
may have unpredictable results and should be avoided.
-
Method Summary
Modifier and TypeMethodDescriptiondoubleReturns the average number of nanoseconds spent loading new values.static CacheStatsempty()Returns a statistics instance where no cache events have been recorded.booleanlongReturns the number of times an entry has been evicted.longReturns the sum of weights of evicted entries.inthashCode()longhitCount()Returns the number of timesCachelookup methods have returned a cached value.doublehitRate()Returns the ratio of cache requests which were hits.longReturns the total number of times thatCachelookup methods attempted to load new values.longReturns the number of timesCachelookup methods failed to load a new value, either because no value was found or an exception was thrown while loading.doubleReturns the ratio of cache loading attempts which threw exceptions.longReturns the number of timesCachelookup methods have successfully loaded a new value.minus(CacheStats other) Returns a newCacheStatsrepresenting the difference between thisCacheStatsandother.longReturns the number of timesCachelookup methods have returned an uncached (newly loaded) value, or null.doublemissRate()Returns the ratio of cache requests which were misses.static CacheStatsof(long hitCount, long missCount, long loadSuccessCount, long loadFailureCount, long totalLoadTime, long evictionCount, long evictionWeight) Returns aCacheStatsrepresenting the specified statistics.plus(CacheStats other) Returns a newCacheStatsrepresenting the sum of thisCacheStatsandother.longReturns the number of timesCachelookup methods have returned either a cached or uncached value.toString()longReturns the total number of nanoseconds the cache has spent loading new values.
-
Method Details
-
of
public static CacheStats of(long hitCount, long missCount, long loadSuccessCount, long loadFailureCount, long totalLoadTime, long evictionCount, long evictionWeight) Returns aCacheStatsrepresenting the specified statistics.- Parameters:
hitCount- the number of cache hitsmissCount- the number of cache missesloadSuccessCount- the number of successful cache loadsloadFailureCount- the number of failed cache loadstotalLoadTime- the total load time (success and failure)evictionCount- the number of entries evicted from the cacheevictionWeight- the sum of weights of entries evicted from the cache- Returns:
- a
CacheStatsrepresenting the specified statistics - Throws:
IllegalArgumentException- if the metric is negative
-
empty
Returns a statistics instance where no cache events have been recorded.- Returns:
- an empty statistics instance
-
requestCount
public long requestCount()Returns the number of timesCachelookup methods have returned either a cached or uncached value. This is defined ashitCount + missCount.Note: the values of the metrics are undefined in case of overflow (though it is guaranteed not to throw an exception). If you require specific handling, we recommend implementing your own stats collector.
- Returns:
- the
hitCount + missCount
-
hitCount
public long hitCount()Returns the number of timesCachelookup methods have returned a cached value.- Returns:
- the number of times
Cachelookup methods have returned a cached value
-
hitRate
public double hitRate()Returns the ratio of cache requests which were hits. This is defined ashitCount / requestCount, or1.0whenrequestCount == 0. Note thathitRate + missRate =~ 1.0.- Returns:
- the ratio of cache requests which were hits
-
missCount
public long missCount()Returns the number of timesCachelookup methods have returned an uncached (newly loaded) value, or null. Multiple concurrent calls toCachelookup methods on an absent value can result in multiple misses, all returning the results of a single cache load operation.- Returns:
- the number of times
Cachelookup methods have returned an uncached (newly loaded) value, or null
-
missRate
public double missRate()Returns the ratio of cache requests which were misses. This is defined asmissCount / requestCount, or0.0whenrequestCount == 0. Note thathitRate + missRate =~ 1.0. Cache misses include all requests which weren't cache hits, including requests which resulted in either successful or failed loading attempts, and requests which waited for other threads to finish loading. It is thus the case thatmissCount >= loadSuccessCount + loadFailureCount. Multiple concurrent misses for the same key will result in a single load operation.- Returns:
- the ratio of cache requests which were misses
-
loadCount
public long loadCount()Returns the total number of times thatCachelookup methods attempted to load new values. This includes both successful load operations, as well as those that threw exceptions. This is defined asloadSuccessCount + loadFailureCount.Note: the values of the metrics are undefined in case of overflow (though it is guaranteed not to throw an exception). If you require specific handling, we recommend implementing your own stats collector.
- Returns:
- the
loadSuccessCount + loadFailureCount
-
loadSuccessCount
public long loadSuccessCount()Returns the number of timesCachelookup methods have successfully loaded a new value. This is always incremented in conjunction withmissCount, thoughmissCountis also incremented when an exception is encountered during cache loading (seeloadFailureCount). Multiple concurrent misses for the same key will result in a single load operation.- Returns:
- the number of times
Cachelookup methods have successfully loaded a new value
-
loadFailureCount
public long loadFailureCount()Returns the number of timesCachelookup methods failed to load a new value, either because no value was found or an exception was thrown while loading. This is always incremented in conjunction withmissCount, thoughmissCountis also incremented when cache loading completes successfully (seeloadSuccessCount). Multiple concurrent misses for the same key will result in a single load operation.- Returns:
- the number of times
Cachelookup methods failed to load a new value
-
loadFailureRate
public double loadFailureRate()Returns the ratio of cache loading attempts which threw exceptions. This is defined asloadFailureCount / (loadSuccessCount + loadFailureCount), or0.0whenloadSuccessCount + loadFailureCount == 0.Note: the values of the metrics are undefined in case of overflow (though it is guaranteed not to throw an exception). If you require specific handling, we recommend implementing your own stats collector.
- Returns:
- the ratio of cache loading attempts which threw exceptions
-
totalLoadTime
public long totalLoadTime()Returns the total number of nanoseconds the cache has spent loading new values. This can be used to calculate the miss penalty. This value is increased every timeloadSuccessCountorloadFailureCountis incremented.- Returns:
- the total number of nanoseconds the cache has spent loading new values
-
averageLoadPenalty
public double averageLoadPenalty()Returns the average number of nanoseconds spent loading new values. This is defined astotalLoadTime / (loadSuccessCount + loadFailureCount).Note: the values of the metrics are undefined in case of overflow (though it is guaranteed not to throw an exception). If you require specific handling, we recommend implementing your own stats collector.
- Returns:
- the average number of nanoseconds spent loading new values
-
evictionCount
public long evictionCount()Returns the number of times an entry has been evicted. This count does not include manual invalidations.- Returns:
- the number of times an entry has been evicted
-
evictionWeight
public long evictionWeight()Returns the sum of weights of evicted entries. This total does not include manual invalidations.- Returns:
- the sum of weights of evicted entities
-
minus
Returns a newCacheStatsrepresenting the difference between thisCacheStatsandother. Negative values, which aren't supported byCacheStatswill be rounded up to zero.- Parameters:
other- the statistics to subtract with- Returns:
- the difference between this instance and
other
-
plus
Returns a newCacheStatsrepresenting the sum of thisCacheStatsandother.Note: the values of the metrics are undefined in case of overflow (though it is guaranteed not to throw an exception). If you require specific handling, we recommend implementing your own stats collector.
- Parameters:
other- the statistics to add with- Returns:
- the sum of the statistics
-
hashCode
public int hashCode() -
equals
-
toString
-