Top Description Fields Constructors Methods
com.github.benmanes.caffeine.cache.stats

public final Class CacheStats

extends Object
Class Inheritance
Annotations
@Immutable
Imports
java.util.Objects, org.checkerframework.checker.index.qual.NonNegative, org.checkerframework.checker.nullness.qual.Nullable, com.github.benmanes.caffeine.cache.Cache, .LoadingCache, com.google.errorprone.annotations.Immutable

Statistics about the performance of a Cache.

Cache statistics are incremented according to the following rules:

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.

Author
[email protected] (Ben Manes)

Field Summary

Modifier and TypeField and Description
private static final CacheStats
private final long
private final long
private final long
private final long
private final long
private final long
private final long

Constructor Summary

AccessConstructor and Description
private
CacheStats(@NonNegative long hitCount, @NonNegative long missCount, @NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, @NonNegative long totalLoadTime, @NonNegative long evictionCount, @NonNegative long evictionWeight)

Method Summary

Modifier and TypeMethod and Description
public @NonNegative double

Returns:

the average number of nanoseconds spent loading new values
averageLoadPenalty
()

Returns the average number of nanoseconds spent loading new values.

public static CacheStats

Returns:

an empty statistics instance
empty
()

Returns a statistics instance where no cache events have been recorded.

public boolean
equals(@Nullable Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public @NonNegative long

Returns:

the number of times an entry has been evicted
evictionCount
()

Returns the number of times an entry has been evicted.

public @NonNegative long

Returns:

the sum of weights of evicted entities
evictionWeight
()

Returns the sum of weights of evicted entries.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public @NonNegative long

Returns:

the number of times Cache lookup methods have returned a cached value
hitCount
()

Returns the number of times Cache lookup methods have returned a cached value.

public @NonNegative double

Returns:

the ratio of cache requests which were hits
hitRate
()

Returns the ratio of cache requests which were hits.

public @NonNegative long

Returns:

the loadSuccessCount + loadFailureCount
loadCount
()

Returns the total number of times that Cache lookup methods attempted to load new values.

public @NonNegative long

Returns:

the number of times Cache lookup methods failed to load a new value
loadFailureCount
()

Returns the number of times Cache lookup methods failed to load a new value, either because no value was found or an exception was thrown while loading.

public @NonNegative double

Returns:

the ratio of cache loading attempts which threw exceptions
loadFailureRate
()

Returns the ratio of cache loading attempts which threw exceptions.

public @NonNegative long

Returns:

the number of times Cache lookup methods have successfully loaded a new value
loadSuccessCount
()

Returns the number of times Cache lookup methods have successfully loaded a new value.

public CacheStats

Returns:

the difference between this instance and other
minus
(CacheStats
the statistics to subtract with
other
)

Returns a new CacheStats representing the difference between this CacheStats and other.

public @NonNegative long

Returns:

the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null
missCount
()

Returns the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null.

public @NonNegative double

Returns:

the ratio of cache requests which were misses
missRate
()

Returns the ratio of cache requests which were misses.

public static CacheStats

Returns:

a CacheStats representing the specified statistics
of
(@NonNegative long
the number of cache hits
hitCount
,
@NonNegative long
the number of cache misses
missCount
,
@NonNegative long
the number of successful cache loads
loadSuccessCount
,
@NonNegative long
the number of failed cache loads
loadFailureCount
,
@NonNegative long
the total load time (success and failure)
totalLoadTime
,
@NonNegative long
the number of entries evicted from the cache
evictionCount
,
@NonNegative long
the sum of weights of entries evicted from the cache
evictionWeight
)

Returns a CacheStats representing the specified statistics.

public CacheStats

Returns:

the sum of the statistics
plus
(CacheStats
the statistics to add with
other
)

Returns a new CacheStats representing the sum of this CacheStats and other.

public @NonNegative long

Returns:

the hitCount + missCount
requestCount
()

Returns the number of times Cache lookup methods have returned either a cached or uncached value.

private static long
saturatedAdd(long a, long b)

Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public @NonNegative long

Returns:

the total number of nanoseconds the cache has spent loading new values
totalLoadTime
()

Returns the total number of nanoseconds the cache has spent loading new values.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

EMPTY_STATSback to summary
private static final CacheStats EMPTY_STATS
evictionCountback to summary
private final long evictionCount
evictionWeightback to summary
private final long evictionWeight
hitCountback to summary
private final long hitCount
loadFailureCountback to summary
private final long loadFailureCount
loadSuccessCountback to summary
private final long loadSuccessCount
missCountback to summary
private final long missCount
totalLoadTimeback to summary
private final long totalLoadTime

Constructor Detail

CacheStatsback to summary
private CacheStats(@NonNegative long hitCount, @NonNegative long missCount, @NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, @NonNegative long totalLoadTime, @NonNegative long evictionCount, @NonNegative long evictionWeight)

Method Detail

averageLoadPenaltyback to summary
public @NonNegative double averageLoadPenalty()

Returns the average number of nanoseconds spent loading new values. This is defined as totalLoadTime / (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:@NonNegative double

the average number of nanoseconds spent loading new values

emptyback to summary
public static CacheStats empty()

Returns a statistics instance where no cache events have been recorded.

Returns:CacheStats

an empty statistics instance

equalsback to summary
public boolean equals(@Nullable Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:@Nullable Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
evictionCountback to summary
public @NonNegative long evictionCount()

Returns the number of times an entry has been evicted. This count does not include manual invalidations.

Returns:@NonNegative long

the number of times an entry has been evicted

evictionWeightback to summary
public @NonNegative long evictionWeight()

Returns the sum of weights of evicted entries. This total does not include manual invalidations.

Returns:@NonNegative long

the sum of weights of evicted entities

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
hitCountback to summary
public @NonNegative long hitCount()

Returns the number of times Cache lookup methods have returned a cached value.

Returns:@NonNegative long

the number of times Cache lookup methods have returned a cached value

hitRateback to summary
public @NonNegative double hitRate()

Returns the ratio of cache requests which were hits. This is defined as hitCount / requestCount, or 1.0 when requestCount == 0. Note that hitRate + missRate =~ 1.0.

Returns:@NonNegative double

the ratio of cache requests which were hits

loadCountback to summary
public @NonNegative long loadCount()

Returns the total number of times that Cache lookup methods attempted to load new values. This includes both successful load operations, as well as those that threw exceptions. This is defined as 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:@NonNegative long

the loadSuccessCount + loadFailureCount

loadFailureCountback to summary
public @NonNegative long loadFailureCount()

Returns the number of times Cache 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 with missCount, though missCount is also incremented when cache loading completes successfully (see loadSuccessCount). Multiple concurrent misses for the same key will result in a single load operation.

Returns:@NonNegative long

the number of times Cache lookup methods failed to load a new value

loadFailureRateback to summary
public @NonNegative double loadFailureRate()

Returns the ratio of cache loading attempts which threw exceptions. This is defined as loadFailureCount / (loadSuccessCount + loadFailureCount), or 0.0 when loadSuccessCount + 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:@NonNegative double

the ratio of cache loading attempts which threw exceptions

loadSuccessCountback to summary
public @NonNegative long loadSuccessCount()

Returns the number of times Cache lookup methods have successfully loaded a new value. This is always incremented in conjunction with missCount, though missCount is also incremented when an exception is encountered during cache loading (see loadFailureCount). Multiple concurrent misses for the same key will result in a single load operation.

Returns:@NonNegative long

the number of times Cache lookup methods have successfully loaded a new value

minusback to summary
public CacheStats minus(CacheStats other)

Returns a new CacheStats representing the difference between this CacheStats and other. Negative values, which aren't supported by CacheStats will be rounded up to zero.

Parameters
other:CacheStats

the statistics to subtract with

Returns:CacheStats

the difference between this instance and other

missCountback to summary
public @NonNegative long missCount()

Returns the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null. Multiple concurrent calls to Cache lookup methods on an absent value can result in multiple misses, all returning the results of a single cache load operation.

Returns:@NonNegative long

the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null

missRateback to summary
public @NonNegative double missRate()

Returns the ratio of cache requests which were misses. This is defined as missCount / requestCount, or 0.0 when requestCount == 0. Note that hitRate + 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 that missCount >= loadSuccessCount + loadFailureCount. Multiple concurrent misses for the same key will result in a single load operation.

Returns:@NonNegative double

the ratio of cache requests which were misses

ofback to summary
public static CacheStats of(@NonNegative long hitCount, @NonNegative long missCount, @NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, @NonNegative long totalLoadTime, @NonNegative long evictionCount, @NonNegative long evictionWeight)

Returns a CacheStats representing the specified statistics.

Parameters
hitCount:@NonNegative long

the number of cache hits

missCount:@NonNegative long

the number of cache misses

loadSuccessCount:@NonNegative long

the number of successful cache loads

loadFailureCount:@NonNegative long

the number of failed cache loads

totalLoadTime:@NonNegative long

the total load time (success and failure)

evictionCount:@NonNegative long

the number of entries evicted from the cache

evictionWeight:@NonNegative long

the sum of weights of entries evicted from the cache

Returns:CacheStats

a CacheStats representing the specified statistics

plusback to summary
public CacheStats plus(CacheStats other)

Returns a new CacheStats representing the sum of this CacheStats and other.

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:CacheStats

the statistics to add with

Returns:CacheStats

the sum of the statistics

requestCountback to summary
public @NonNegative long requestCount()

Returns the number of times Cache lookup methods have returned either a cached or uncached value. This is defined as hitCount + 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:@NonNegative long

the hitCount + missCount

saturatedAddback to summary
private static long saturatedAdd(long a, long b)

Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.

Annotations
@SuppressWarnings:ShortCircuitBoolean
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
totalLoadTimeback to summary
public @NonNegative 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 time loadSuccessCount or loadFailureCount is incremented.

Returns:@NonNegative long

the total number of nanoseconds the cache has spent loading new values