Class CacheStats

java.lang.Object
com.github.benmanes.caffeine.cache.stats.CacheStats

@Immutable public final class CacheStats extends Object
Statistics about the performance of a Cache.

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 loadFailureCount 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 computed through the asMap the loadSuccessCount or loadFailureCount is incremented.
  • When an entry is evicted from the cache, evictionCount is incremented and the weight added to evictionWeight.
  • 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.

  • Field Summary

    Fields
    Modifier and Type
    Field
    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

    Constructors
    Constructor
    Description
    CacheStats(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount)
    Deprecated.
    This constructor is scheduled for removal in version 3.0.0.
    CacheStats(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount, @org.checkerframework.checker.index.qual.NonNegative long evictionWeight)
    Deprecated.
  • Method Summary

    Modifier and Type
    Method
    Description
    @org.checkerframework.checker.index.qual.NonNegative double
    Returns the average time spent loading new values.
    static @NonNull CacheStats
    Returns a statistics instance where no cache events have been recorded.
    boolean
     
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the number of times an entry has been evicted.
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the sum of weights of evicted entries.
    int
     
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the number of times Cache lookup methods have returned a cached value.
    @org.checkerframework.checker.index.qual.NonNegative double
    Returns the ratio of cache requests which were hits.
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the total number of times that Cache lookup methods attempted to load new values.
    @org.checkerframework.checker.index.qual.NonNegative long
    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.
    @org.checkerframework.checker.index.qual.NonNegative double
    Returns the ratio of cache loading attempts which threw exceptions.
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the number of times Cache lookup methods have successfully loaded a new value.
    @NonNull CacheStats
    minus(@NonNull CacheStats other)
    Returns a new CacheStats representing the difference between this CacheStats and other.
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null.
    @org.checkerframework.checker.index.qual.NonNegative double
    Returns the ratio of cache requests which were misses.
    static CacheStats
    of(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount, @org.checkerframework.checker.index.qual.NonNegative long evictionWeight)
    Returns a CacheStats representing the specified statistics.
    @NonNull CacheStats
    plus(@NonNull CacheStats other)
    Returns a new CacheStats representing the sum of this CacheStats and other.
    @org.checkerframework.checker.index.qual.NonNegative long
    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.
    private static long
    saturatedSubtract(long a, long b)
    Returns the difference of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
     
    @org.checkerframework.checker.index.qual.NonNegative long
    Returns the total number of nanoseconds the cache has spent loading new values.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • 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 Details

    • CacheStats

      @Deprecated public CacheStats(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount)
      Deprecated.
      This constructor is scheduled for removal in version 3.0.0.
      Constructs a new CacheStats instance.
      Parameters:
      hitCount - the number of cache hits
      missCount - the number of cache misses
      loadSuccessCount - the number of successful cache loads
      loadFailureCount - the number of failed cache loads
      totalLoadTime - the total load time (success and failure)
      evictionCount - the number of entries evicted from the cache
    • CacheStats

      @Deprecated public CacheStats(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount, @org.checkerframework.checker.index.qual.NonNegative long evictionWeight)
      Deprecated.
      Constructs a new CacheStats 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 hits
      missCount - the number of cache misses
      loadSuccessCount - the number of successful cache loads
      loadFailureCount - the number of failed cache loads
      totalLoadTime - the total load time (success and failure)
      evictionCount - the number of entries evicted from the cache
      evictionWeight - the sum of weights of entries evicted from the cache
  • Method Details

    • of

      public static CacheStats of(@org.checkerframework.checker.index.qual.NonNegative long hitCount, @org.checkerframework.checker.index.qual.NonNegative long missCount, @org.checkerframework.checker.index.qual.NonNegative long loadSuccessCount, @org.checkerframework.checker.index.qual.NonNegative long loadFailureCount, @org.checkerframework.checker.index.qual.NonNegative long totalLoadTime, @org.checkerframework.checker.index.qual.NonNegative long evictionCount, @org.checkerframework.checker.index.qual.NonNegative long evictionWeight)
      Returns a CacheStats representing the specified statistics.
      Parameters:
      hitCount - the number of cache hits
      missCount - the number of cache misses
      loadSuccessCount - the number of successful cache loads
      loadFailureCount - the number of failed cache loads
      totalLoadTime - the total load time (success and failure)
      evictionCount - the number of entries evicted from the cache
      evictionWeight - the sum of weights of entries evicted from the cache
    • empty

      public static @NonNull CacheStats empty()
      Returns a statistics instance where no cache events have been recorded.
      Returns:
      an empty statistics instance
    • requestCount

      public @org.checkerframework.checker.index.qual.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:
      the hitCount + missCount
    • hitCount

      public @org.checkerframework.checker.index.qual.NonNegative long hitCount()
      Returns the number of times Cache lookup methods have returned a cached value.
      Returns:
      the number of times Cache lookup methods have returned a cached value
    • hitRate

      public @org.checkerframework.checker.index.qual.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:
      the ratio of cache requests which were hits
    • missCount

      public @org.checkerframework.checker.index.qual.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:
      the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null
    • missRate

      public @org.checkerframework.checker.index.qual.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:
      the ratio of cache requests which were misses
    • loadCount

      public @org.checkerframework.checker.index.qual.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:
      the loadSuccessCount + loadFailureCount
    • loadSuccessCount

      public @org.checkerframework.checker.index.qual.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:
      the number of times Cache lookup methods have successfully loaded a new value
    • loadFailureCount

      public @org.checkerframework.checker.index.qual.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:
      the number of times Cache lookup methods failed to load a new value
    • loadFailureRate

      public @org.checkerframework.checker.index.qual.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:
      the ratio of cache loading attempts which threw exceptions
    • totalLoadTime

      public @org.checkerframework.checker.index.qual.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:
      the total number of nanoseconds the cache has spent loading new values
    • averageLoadPenalty

      public @org.checkerframework.checker.index.qual.NonNegative double averageLoadPenalty()
      Returns the average time 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:
      the average time spent loading new values
    • evictionCount

      public @org.checkerframework.checker.index.qual.NonNegative 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 @org.checkerframework.checker.index.qual.NonNegative 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

      public @NonNull CacheStats minus(@NonNull 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 - the statistics to subtract with
      Returns:
      the difference between this instance and other
    • plus

      public @NonNull CacheStats plus(@NonNull 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 - the statistics to add with
      Returns:
      the sum of the statistics
    • saturatedSubtract

      private static long saturatedSubtract(long a, long b)
      Returns the difference of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.
    • saturatedAdd

      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.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object