Class ConcurrentStatsCounter
- java.lang.Object
-
- com.github.benmanes.caffeine.cache.stats.ConcurrentStatsCounter
-
- All Implemented Interfaces:
StatsCounter
public final class ConcurrentStatsCounter extends java.lang.Object implements StatsCounter
A thread-safeStatsCounter
implementation for use byCache
implementors.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.LongAdder
evictionCount
private java.util.concurrent.atomic.LongAdder
evictionWeight
private java.util.concurrent.atomic.LongAdder
hitCount
private java.util.concurrent.atomic.LongAdder
loadFailureCount
private java.util.concurrent.atomic.LongAdder
loadSuccessCount
private java.util.concurrent.atomic.LongAdder
missCount
private java.util.concurrent.atomic.LongAdder
totalLoadTime
-
Constructor Summary
Constructors Constructor Description ConcurrentStatsCounter()
Constructs an instance with all counts initialized to zero.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
incrementBy(@NonNull StatsCounter other)
Increments all counters by the values inother
.private static long
negativeToMaxValue(long value)
Returnsvalue
, if non-negative.void
recordEviction()
Deprecated.void
recordEviction(int weight)
Deprecated.void
recordEviction(int weight, RemovalCause cause)
Records the eviction of an entry from the cache.void
recordHits(int count)
Records cache hits.void
recordLoadFailure(long loadTime)
Records the failed load of a new entry.void
recordLoadSuccess(long loadTime)
Records the successful load of a new entry.void
recordMisses(int count)
Records cache misses.CacheStats
snapshot()
Returns a snapshot of this counter's values.java.lang.String
toString()
-
-
-
Field Detail
-
hitCount
private final java.util.concurrent.atomic.LongAdder hitCount
-
missCount
private final java.util.concurrent.atomic.LongAdder missCount
-
loadSuccessCount
private final java.util.concurrent.atomic.LongAdder loadSuccessCount
-
loadFailureCount
private final java.util.concurrent.atomic.LongAdder loadFailureCount
-
totalLoadTime
private final java.util.concurrent.atomic.LongAdder totalLoadTime
-
evictionCount
private final java.util.concurrent.atomic.LongAdder evictionCount
-
evictionWeight
private final java.util.concurrent.atomic.LongAdder evictionWeight
-
-
Method Detail
-
recordHits
public void recordHits(int count)
Description copied from interface:StatsCounter
Records cache hits. This should be called when a cache request returns a cached value.- Specified by:
recordHits
in interfaceStatsCounter
- Parameters:
count
- the number of hits to record
-
recordMisses
public void recordMisses(int count)
Description copied from interface:StatsCounter
Records cache misses. This should be called when a cache request returns a value that was not found in the cache. This method should be called by the loading thread, as well as by threads blocking on the load. Multiple concurrent calls toCache
lookup methods with the same key on an absent value should result in a single call to eitherrecordLoadSuccess
orrecordLoadFailure
and multiple calls to this method, despite all being served by the results of a single load operation.- Specified by:
recordMisses
in interfaceStatsCounter
- Parameters:
count
- the number of misses to record
-
recordLoadSuccess
public void recordLoadSuccess(long loadTime)
Description copied from interface:StatsCounter
Records the successful load of a new entry. This method should be called when a cache request causes an entry to be loaded (such as byCache.get(K, java.util.function.Function<? super K, ? extends V>)
orMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
) and the loading completes successfully. In contrast toStatsCounter.recordMisses(@org.checkerframework.checker.index.qual.NonNegative int)
, this method should only be called by the loading thread.- Specified by:
recordLoadSuccess
in interfaceStatsCounter
- Parameters:
loadTime
- the number of nanoseconds the cache spent computing or retrieving the new value
-
recordLoadFailure
public void recordLoadFailure(long loadTime)
Description copied from interface:StatsCounter
Records the failed load of a new entry. This method should be called when a cache request causes an entry to be loaded (such as byCache.get(K, java.util.function.Function<? super K, ? extends V>)
orMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
), but an exception is thrown while loading the entry or the loading function returns null. In contrast toStatsCounter.recordMisses(@org.checkerframework.checker.index.qual.NonNegative int)
, this method should only be called by the loading thread.- Specified by:
recordLoadFailure
in interfaceStatsCounter
- Parameters:
loadTime
- the number of nanoseconds the cache spent computing or retrieving the new value prior to discovering the value doesn't exist or an exception being thrown
-
recordEviction
@Deprecated public void recordEviction()
Deprecated.Description copied from interface:StatsCounter
Records the eviction of an entry from the cache. This should only been called when an entry is evicted due to the cache's eviction strategy, and not as a result of manualinvalidations
.- Specified by:
recordEviction
in interfaceStatsCounter
-
recordEviction
@Deprecated public void recordEviction(int weight)
Deprecated.Description copied from interface:StatsCounter
Records the eviction of an entry from the cache. This should only been called when an entry is evicted due to the cache's eviction strategy, and not as a result of manualinvalidations
.- Specified by:
recordEviction
in interfaceStatsCounter
- Parameters:
weight
- the weight of the evicted entry
-
recordEviction
public void recordEviction(int weight, RemovalCause cause)
Description copied from interface:StatsCounter
Records the eviction of an entry from the cache. This should only been called when an entry is evicted due to the cache's eviction strategy, and not as a result of manualinvalidations
.- Specified by:
recordEviction
in interfaceStatsCounter
- Parameters:
weight
- the weight of the evicted entrycause
- the reason for which the entry was removed
-
snapshot
public CacheStats snapshot()
Description copied from interface:StatsCounter
Returns a snapshot of this counter's values. Note that this may be an inconsistent view, as it may be interleaved with update operations.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.
- Specified by:
snapshot
in interfaceStatsCounter
- Returns:
- a snapshot of this counter's values
-
negativeToMaxValue
private static long negativeToMaxValue(long value)
Returnsvalue
, if non-negative. Otherwise, returnsLong.MAX_VALUE
.
-
incrementBy
public void incrementBy(@NonNull StatsCounter other)
Increments all counters by the values inother
.- Parameters:
other
- the counter to increment from
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-