-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CacheStats should be a value-based class
This deprecates the constructor and introduces a static factory to obtain an instance. The allocation cost can then be reduced when Project Valhalla (JSR-169) is delivered.
- Loading branch information
Showing
9 changed files
with
60 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,12 +53,16 @@ | |
* A lookup is specifically defined as an invocation of one of the methods | ||
* {@link LoadingCache#get(Object)}, {@link Cache#get(Object, java.util.function.Function)}, or | ||
* {@link LoadingCache#getAll(Iterable)}. | ||
* <p> | ||
* This is a <em>value-based</em> class; use of identity-sensitive operations (including reference | ||
* equality ({@code ==}), identity hash code, or synchronization) on instances of {@code CacheStats} | ||
* may have unpredictable results and should be avoided. | ||
* | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Immutable | ||
public final class CacheStats { | ||
private static final CacheStats EMPTY_STATS = new CacheStats(0, 0, 0, 0, 0, 0, 0); | ||
private static final CacheStats EMPTY_STATS = CacheStats.of(0L, 0L, 0L, 0L, 0L, 0L, 0L); | ||
|
||
private final long hitCount; | ||
private final long missCount; | ||
|
@@ -100,6 +104,7 @@ public CacheStats(@NonNegative long hitCount, @NonNegative long missCount, | |
* @param evictionCount the number of entries evicted from the cache | ||
* @param evictionWeight the sum of weights of entries evicted from the cache | ||
*/ | ||
@Deprecated | ||
public CacheStats(@NonNegative long hitCount, @NonNegative long missCount, | ||
@NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, | ||
@NonNegative long totalLoadTime, @NonNegative long evictionCount, | ||
|
@@ -117,6 +122,27 @@ public CacheStats(@NonNegative long hitCount, @NonNegative long missCount, | |
this.evictionWeight = evictionWeight; | ||
} | ||
|
||
/** | ||
* Returns a {@code CacheStats} representing the specified statistics. | ||
* | ||
* @param hitCount the number of cache hits | ||
* @param missCount the number of cache misses | ||
* @param loadSuccessCount the number of successful cache loads | ||
* @param loadFailureCount the number of failed cache loads | ||
* @param totalLoadTime the total load time (success and failure) | ||
* @param evictionCount the number of entries evicted from the cache | ||
* @param evictionWeight the sum of weights of entries evicted from the cache | ||
*/ | ||
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) { | ||
// 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. | ||
return new CacheStats(hitCount, missCount, loadSuccessCount, | ||
loadFailureCount, totalLoadTime, evictionCount, evictionWeight); | ||
} | ||
|
||
/** | ||
* Returns a statistics instance where no cache events have been recorded. | ||
* | ||
|
@@ -317,7 +343,7 @@ public long evictionWeight() { | |
*/ | ||
@NonNull | ||
public CacheStats minus(@NonNull CacheStats other) { | ||
return new CacheStats( | ||
return CacheStats.of( | ||
Math.max(0L, saturatedSubtract(hitCount, other.hitCount)), | ||
Math.max(0L, saturatedSubtract(missCount, other.missCount)), | ||
Math.max(0L, saturatedSubtract(loadSuccessCount, other.loadSuccessCount)), | ||
|
@@ -340,7 +366,7 @@ public CacheStats minus(@NonNull CacheStats other) { | |
*/ | ||
@NonNull | ||
public CacheStats plus(@NonNull CacheStats other) { | ||
return new CacheStats( | ||
return CacheStats.of( | ||
saturatedAdd(hitCount, other.hitCount), | ||
saturatedAdd(missCount, other.missCount), | ||
saturatedAdd(loadSuccessCount, other.loadSuccessCount), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters