|
@@ -76,14 +76,6 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
return l > r? 1: l < r? -1: 0;
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
- /** A clock for measuring time so that it can be mocked in unit tests. */
|
|
|
- static class Clock {
|
|
|
- /** @return the current time. */
|
|
|
- long currentTime() {
|
|
|
- return System.nanoTime();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
private static int updateRecommendedLength(int recommendedLength,
|
|
|
int sizeLimit) {
|
|
@@ -102,7 +94,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
private final long creationExpirationPeriod;
|
|
|
private final long accessExpirationPeriod;
|
|
|
private final int sizeLimit;
|
|
|
- private final Clock clock;
|
|
|
+ private final Timer timer;
|
|
|
|
|
|
/**
|
|
|
* @param recommendedLength Recommended size of the internal array.
|
|
@@ -120,7 +112,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
final long creationExpirationPeriod,
|
|
|
final long accessExpirationPeriod) {
|
|
|
this(recommendedLength, sizeLimit,
|
|
|
- creationExpirationPeriod, accessExpirationPeriod, new Clock());
|
|
|
+ creationExpirationPeriod, accessExpirationPeriod, new Timer());
|
|
|
}
|
|
|
|
|
|
@VisibleForTesting
|
|
@@ -128,7 +120,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
final int sizeLimit,
|
|
|
final long creationExpirationPeriod,
|
|
|
final long accessExpirationPeriod,
|
|
|
- final Clock clock) {
|
|
|
+ final Timer timer) {
|
|
|
super(updateRecommendedLength(recommendedLength, sizeLimit));
|
|
|
|
|
|
this.sizeLimit = sizeLimit;
|
|
@@ -147,11 +139,11 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
|
|
|
this.queue = new PriorityQueue<Entry>(
|
|
|
sizeLimit > 0? sizeLimit + 1: 1 << 10, expirationTimeComparator);
|
|
|
- this.clock = clock;
|
|
|
+ this.timer = timer;
|
|
|
}
|
|
|
|
|
|
void setExpirationTime(final Entry e, final long expirationPeriod) {
|
|
|
- e.setExpirationTime(clock.currentTime() + expirationPeriod);
|
|
|
+ e.setExpirationTime(timer.monotonicNowNanos() + expirationPeriod);
|
|
|
}
|
|
|
|
|
|
boolean isExpired(final Entry e, final long now) {
|
|
@@ -168,7 +160,7 @@ public class LightWeightCache<K, E extends K> extends LightWeightGSet<K, E> {
|
|
|
|
|
|
/** Evict expired entries. */
|
|
|
private void evictExpiredEntries() {
|
|
|
- final long now = clock.currentTime();
|
|
|
+ final long now = timer.monotonicNowNanos();
|
|
|
for(int i = 0; i < EVICTION_LIMIT; i++) {
|
|
|
final Entry peeked = queue.peek();
|
|
|
if (peeked == null || !isExpired(peeked, now)) {
|