Class CacheStats
- java.lang.Object
-
- com.github.benmanes.caffeine.cache.stats.CacheStats
-
@Immutable public final class CacheStats extends java.lang.Object
Statistics about the performance of aCache
.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
andloadSuccessCount
are incremented, and the total loading time, in nanoseconds, is added tototalLoadTime
. - When an exception is thrown while loading an entry,
missCount
andloadFailureCount
are 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
loadSuccessCount
orloadFailureCount
is incremented. - When an entry is evicted from the cache,
evictionCount
is 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)
, orLoadingCache.getAll(Iterable)
. - When a cache lookup encounters an existing cache entry
-
-
Field Summary
Fields Modifier and Type Field Description private static CacheStats
EMPTY_STATS
private long
evictionCount
private long
evictionWeight
private long
hitCount
private long
loadFailureCount
private long
loadSuccessCount
private long
missCount
private long
totalLoadTime
-
Constructor Summary
Constructors Constructor Description CacheStats(long hitCount, long missCount, long loadSuccessCount, long loadFailureCount, long totalLoadTime, long evictionCount)
Deprecated.This constructor is scheduled for removal in version 3.0.0.CacheStats(long hitCount, long missCount, long loadSuccessCount, long loadFailureCount, long totalLoadTime, long evictionCount, long evictionWeight)
Constructs a newCacheStats
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
averageLoadPenalty()
Returns the average time spent loading new values.static CacheStats
empty()
Returns a statistics instance where no cache events have been recorded.boolean
equals(java.lang.Object o)
long
evictionCount()
Returns the number of times an entry has been evicted.long
evictionWeight()
Returns the sum of weights of evicted entries.int
hashCode()
long
hitCount()
Returns the number of timesCache
lookup methods have returned a cached value.double
hitRate()
Returns the ratio of cache requests which were hits.long
loadCount()
Returns the total number of times thatCache
lookup methods attempted to load new values.long
loadFailureCount()
Returns the number of timesCache
lookup methods failed to load a new value, either because no value was found or an exception was thrown while loading.double
loadFailureRate()
Returns the ratio of cache loading attempts which threw exceptions.long
loadSuccessCount()
Returns the number of timesCache
lookup methods have successfully loaded a new value.CacheStats
minus(CacheStats other)
Returns a newCacheStats
representing the difference between thisCacheStats
andother
.long
missCount()
Returns the number of timesCache
lookup methods have returned an uncached (newly loaded) value, or null.double
missRate()
Returns the ratio of cache requests which were misses.CacheStats
plus(CacheStats other)
Returns a newCacheStats
representing the sum of thisCacheStats
andother
.long
requestCount()
Returns the number of timesCache
lookup methods have returned either a cached or uncached value.java.lang.String
toString()
long
totalLoadTime()
Returns the total number of nanoseconds the cache has spent loading new values.
-
-
-
Field Detail
-
EMPTY_STATS
private static final CacheStats EMPTY_STATS
-
hitCount
private final long hitCount
-
missCount
private final long missCount
-
loadSuccessCount
private final long loadSuccessCount
-
loadFailureCount
private final long loadFailureCount
-
totalLoadTime
private final long totalLoadTime
-
evictionCount
private final long evictionCount
-
evictionWeight
private final long evictionWeight
-
-
Constructor Detail
-
CacheStats
@Deprecated public CacheStats(@Nonnegative long hitCount, @Nonnegative long missCount, @Nonnegative long loadSuccessCount, @Nonnegative long loadFailureCount, @Nonnegative long totalLoadTime, @Nonnegative long evictionCount)
Deprecated.This constructor is scheduled for removal in version 3.0.0.Constructs a newCacheStats
instance.- 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 cache
-
CacheStats
public CacheStats(@Nonnegative long hitCount, @Nonnegative long missCount, @Nonnegative long loadSuccessCount, @Nonnegative long loadFailureCount, @Nonnegative long totalLoadTime, @Nonnegative long evictionCount, @Nonnegative long evictionWeight)
Constructs a newCacheStats
instance.Many parameters of the same type in a row is a bad thing, but this class is not constructed by end users and is too fine-grained for a builder.
- 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
-
-
Method Detail
-
empty
@Nonnull public static CacheStats empty()
Returns a statistics instance where no cache events have been recorded.- Returns:
- an empty statistics instance
-
requestCount
@Nonnegative public long requestCount()
Returns the number of timesCache
lookup methods have returned either a cached or uncached value. This is defined ashitCount + missCount
.- Returns:
- the
hitCount + missCount
-
hitCount
@Nonnegative public long hitCount()
Returns the number of timesCache
lookup methods have returned a cached value.- Returns:
- the number of times
Cache
lookup methods have returned a cached value
-
hitRate
@Nonnegative public double hitRate()
Returns the ratio of cache requests which were hits. This is defined ashitCount / requestCount
, or1.0
whenrequestCount == 0
. Note thathitRate + missRate =~ 1.0
.- Returns:
- the ratio of cache requests which were hits
-
missCount
@Nonnegative public long missCount()
Returns the number of timesCache
lookup methods have returned an uncached (newly loaded) value, or null. Multiple concurrent calls toCache
lookup 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
Cache
lookup methods have returned an uncached (newly loaded) value, or null
-
missRate
@Nonnegative public double missRate()
Returns the ratio of cache requests which were misses. This is defined asmissCount / requestCount
, or0.0
whenrequestCount == 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
@Nonnegative public long loadCount()
Returns the total number of times thatCache
lookup methods attempted to load new values. This includes both successful load operations, as well as those that threw exceptions. This is defined asloadSuccessCount + loadFailureCount
.- Returns:
- the
loadSuccessCount + loadFailureCount
-
loadSuccessCount
@Nonnegative public long loadSuccessCount()
Returns the number of timesCache
lookup methods have successfully loaded a new value. This is always incremented in conjunction withmissCount
, thoughmissCount
is 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
Cache
lookup methods have successfully loaded a new value
-
loadFailureCount
@Nonnegative public long loadFailureCount()
Returns the number of timesCache
lookup 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
, thoughmissCount
is 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
Cache
lookup methods failed to load a new value
-
loadFailureRate
@Nonnegative public double loadFailureRate()
Returns the ratio of cache loading attempts which threw exceptions. This is defined asloadFailureCount / (loadSuccessCount + loadFailureCount)
, or0.0
whenloadSuccessCount + loadFailureCount == 0
.- Returns:
- the ratio of cache loading attempts which threw exceptions
-
totalLoadTime
@Nonnegative 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 timeloadSuccessCount
orloadFailureCount
is incremented.- Returns:
- the total number of nanoseconds the cache has spent loading new values
-
averageLoadPenalty
@Nonnegative public double averageLoadPenalty()
Returns the average time spent loading new values. This is defined astotalLoadTime / (loadSuccessCount + loadFailureCount)
.- Returns:
- the average time spent loading new values
-
evictionCount
@Nonnegative 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
@Nonnegative 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
@Nonnull public CacheStats minus(@Nonnull CacheStats other)
Returns a newCacheStats
representing the difference between thisCacheStats
andother
. Negative values, which aren't supported byCacheStats
will be rounded up to zero.- Parameters:
other
- the statistics to subtract with- Returns:
- the difference between this instance and
other
-
plus
@Nonnull public CacheStats plus(@Nonnull CacheStats other)
Returns a newCacheStats
representing the sum of thisCacheStats
andother
.- Parameters:
other
- the statistics to add with- Returns:
- the sum of the statistics
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-