use long instead of Instant for time

Working with longs is faster and requires less
cache. The space in L123 caches is precious.
This commit is contained in:
2019-08-19 18:58:24 +02:00
parent feda901f6d
commit 00c20dae6b
2 changed files with 70 additions and 73 deletions

View File

@@ -2,7 +2,6 @@ package org.lucares.utils.cache;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Set;
@@ -31,8 +30,16 @@ import org.slf4j.LoggerFactory;
* Caching frameworks like EhCache only evict entries when a new entry is added.
* That might not be desired, e.g. when the cached objects block resources.
* <p>
* This cache is a simple wrapper for a ConcurrentHashMap that evicts entries
* after timeToLive+5s.
* This cache is a wrapper for a ConcurrentHashMap that evicts entries after the
* specified timeToLive. The cache spawns two threads.
* <ol>
* <li>One for updating time. It is far too expensive to call
* {@link System#currentTimeMillis()} or something similar whenever a timestamp
* is needed. Therefore we have a thread that updates a member periodically.
* <li>One for evicting elements. So that elements can be evicted based on their
* time to live. Other cache implementations do this only when elements are
* added to the cache.
* </ol>
*/
public class HotEntryCache<K, V> {
@@ -67,11 +74,11 @@ public class HotEntryCache<K, V> {
}
private final static class Entry<V> {
private Instant lastAccessed;
private long lastAccessed;
private V value;
public Entry(final V value, final Instant creationTime) {
public Entry(final V value, final long creationTime) {
this.value = value;
lastAccessed = creationTime;
}
@@ -84,11 +91,11 @@ public class HotEntryCache<K, V> {
this.value = value;
}
public Instant getLastAccessed() {
public long getLastAccessed() {
return lastAccessed;
}
public void touch(final Instant instant) {
public void touch(final long instant) {
lastAccessed = instant;
}
}
@@ -110,8 +117,8 @@ public class HotEntryCache<K, V> {
}
}
public void setUpdateInterval(final Duration updateInterval) {
this.updateInterval = Math.max(updateInterval.toMillis(), 1);
public void setUpdateInterval(final long updateInterval) {
this.updateInterval = Math.max(updateInterval, 1);
interrupt();
}
@@ -147,8 +154,8 @@ public class HotEntryCache<K, V> {
private final AtomicReference<CompletableFuture<Void>> future = new AtomicReference<>(null);
private final Object lock = new Object();
private Duration minSleepPeriod = Duration.ofSeconds(5);
private Duration maxSleepPeriod = Duration.ofDays(1);
private long minSleepPeriodInMs = Duration.ofSeconds(5).toMillis();
private long maxSleepPeriodInMs = Duration.ofDays(1).toMillis();
public EvictionThread() {
setDaemon(true);
@@ -161,17 +168,17 @@ public class HotEntryCache<K, V> {
}
}
private Duration getMinSleepPeriod() {
return minSleepPeriod;
private long getMinSleepPeriod() {
return minSleepPeriodInMs;
}
private Duration getMaxSleepPeriod() {
return maxSleepPeriod;
private long getMaxSleepPeriod() {
return maxSleepPeriodInMs;
}
@Override
public void run() {
Duration timeToNextEviction = maxSleepPeriod;
long timeToNextEviction = maxSleepPeriodInMs;
while (true) {
sleepToNextEviction(timeToNextEviction);
@@ -179,7 +186,7 @@ public class HotEntryCache<K, V> {
final CompletableFuture<Void> future = this.future.getAcquire();
try {
final Instant minNextEvictionTime = evictStaleEntries();
final long minNextEvictionTime = evictStaleEntries();
timeToNextEviction = normalizeDurationToNextEviction(minNextEvictionTime);
@@ -194,34 +201,34 @@ public class HotEntryCache<K, V> {
}
}
private Duration normalizeDurationToNextEviction(final Instant minNextEvictionTime) {
Duration timeToNextEviction;
if (!minNextEvictionTime.equals(Instant.MAX)) {
timeToNextEviction = minSleepPeriod;
private long normalizeDurationToNextEviction(final long minNextEvictionTime) {
long timeToNextEviction;
if (minNextEvictionTime != Long.MAX_VALUE) {
timeToNextEviction = minSleepPeriodInMs;
} else {
final Instant now = Instant.now();
timeToNextEviction = Duration.between(now, minNextEvictionTime);
final long now = System.currentTimeMillis();
timeToNextEviction = minNextEvictionTime - now;
}
return timeToNextEviction;
}
private Instant evictStaleEntries() {
Instant minNextEvictionTime = Instant.MAX;
private long evictStaleEntries() {
long minNextEvictionTime = Long.MAX_VALUE;
final Set<HotEntryCache<?, ?>> caches = new HashSet<>();
synchronized (lock) {
caches.addAll(weakCaches.keySet());
}
for (final HotEntryCache<?, ?> cache : caches) {
final Instant nextEvictionTime = cache.evict();
minNextEvictionTime = min(minNextEvictionTime, nextEvictionTime);
final long nextEvictionTime = cache.evict();
minNextEvictionTime = Math.min(minNextEvictionTime, nextEvictionTime);
}
return minNextEvictionTime;
}
private void sleepToNextEviction(final Duration timeToNextEviction) {
private void sleepToNextEviction(final long timeToNextEviction) {
try {
final Duration timeToSleep = minDuration(timeToNextEviction, maxSleepPeriod);
final long timeToSleepMS = Math.max(timeToSleep.toMillis(), minSleepPeriod.toMillis());
final long timeToSleep = Math.min(timeToNextEviction, maxSleepPeriodInMs);
final long timeToSleepMS = Math.max(timeToSleep, minSleepPeriodInMs);
LOGGER.trace("sleeping {}ms", timeToSleepMS);
TimeUnit.MILLISECONDS.sleep(timeToSleepMS);
} catch (final InterruptedException e) {
@@ -247,12 +254,12 @@ public class HotEntryCache<K, V> {
return result;
}
public void setMinSleepPeriod(final Duration minSleepPeriod) {
this.minSleepPeriod = minSleepPeriod;
public void setMinSleepPeriod(final long minSleepPeriodInMs) {
this.minSleepPeriodInMs = minSleepPeriodInMs;
}
public void setMaxSleepPeriod(final Duration maxSleepPeriod) {
this.maxSleepPeriod = maxSleepPeriod;
public void setMaxSleepPeriod(final long maxSleepPeriodInMs) {
this.maxSleepPeriodInMs = maxSleepPeriodInMs;
}
}
@@ -265,7 +272,7 @@ public class HotEntryCache<K, V> {
TIME_UPDATER.start();
}
private static Instant now;
private static long now;
/**
* Mapping of the key to the value.
@@ -286,7 +293,7 @@ public class HotEntryCache<K, V> {
this.timeToLive = timeToLive;
this.clock = clock;
this.name = name;
now = Instant.now(clock);
now = clock.millis();
EVICTER.addCache(this);
TIME_UPDATER.addCache(this);
@@ -316,20 +323,20 @@ public class HotEntryCache<K, V> {
listeners.add(listener);
}
static void setMinSleepPeriod(final Duration minSleepPeriod) {
EVICTER.setMinSleepPeriod(minSleepPeriod);
TIME_UPDATER.setUpdateInterval(minSleepPeriod.dividedBy(2));
static void setMinSleepPeriod(final long minSleepPeriodInMs) {
EVICTER.setMinSleepPeriod(minSleepPeriodInMs);
TIME_UPDATER.setUpdateInterval(minSleepPeriodInMs / 2);
}
static void setMaxSleepPeriod(final Duration maxSleepPeriod) {
EVICTER.setMaxSleepPeriod(maxSleepPeriod);
static void setMaxSleepPeriod(final long maxSleepPeriodInMs) {
EVICTER.setMaxSleepPeriod(maxSleepPeriodInMs);
}
static Duration getMinSleepPeriod() {
static long getMinSleepPeriod() {
return EVICTER.getMinSleepPeriod();
}
static Duration getMaxSleepPeriod() {
static long getMaxSleepPeriod() {
return EVICTER.getMaxSleepPeriod();
}
@@ -354,7 +361,7 @@ public class HotEntryCache<K, V> {
oldEntry.setValue(value);
entry = oldEntry;
} else {
final Instant creationTime = now();
final long creationTime = now();
entry = new Entry<>(value, creationTime);
}
touch(k, entry);
@@ -387,7 +394,7 @@ public class HotEntryCache<K, V> {
final boolean wasEmptyBefore = cache.isEmpty();
final Entry<V> entry = cache.computeIfAbsent(key, (k) -> {
final V value = mappingFunction.apply(k);
final Instant creationTime = now();
final long creationTime = now();
final Entry<V> e = new Entry<>(value, creationTime);
touch(key, e);
return e;
@@ -426,20 +433,18 @@ public class HotEntryCache<K, V> {
});
}
private Instant evict() {
final Instant now = now();
final Instant oldestValuesToKeep = now.minus(timeToLive);
Instant lastAccessTime = Instant.MAX;
private long evict() {
final long now = now();
final long oldestValuesToKeep = now - timeToLive.toMillis();
long lastAccessTime = Long.MAX_VALUE;
LOGGER.trace("{}: cache size before eviction {}", name, cache.size());
// for (final java.util.Map.Entry<Instant, Set<K>> mapEntry :
// lastAccessMap.entrySet()) {
for (final java.util.Map.Entry<K, Entry<V>> mapEntry : cache.entrySet()) {
final Entry<V> entry = mapEntry.getValue();
final Instant lastAccessed = entry.getLastAccessed();
lastAccessTime = min(lastAccessTime, lastAccessed);
final long lastAccessed = entry.getLastAccessed();
lastAccessTime = Math.min(lastAccessTime, lastAccessed);
if (lastAccessed.isAfter(oldestValuesToKeep)) {
if (lastAccessed > oldestValuesToKeep) {
continue;
}
@@ -456,38 +461,30 @@ public class HotEntryCache<K, V> {
}
LOGGER.trace("{}: cache size after eviction {}", name, cache.size());
final Instant nextEvictionTime = lastAccessTime.equals(Instant.MAX) ? Instant.MAX
: lastAccessTime.plus(timeToLive);
final long nextEvictionTime = lastAccessTime == Long.MAX_VALUE ? Long.MAX_VALUE
: lastAccessTime + timeToLive.toMillis();
return nextEvictionTime;
}
private static Instant min(final Instant a, final Instant b) {
return a.isBefore(b) ? a : b;
}
private static Duration minDuration(final Duration a, final Duration b) {
return a.compareTo(b) < 0 ? a : b;
}
private Instant now() {
private long now() {
return now;
}
// visible for test
void updateTime() {
now = Instant.now(clock);
now = clock.millis();
}
private void touch(final K key, final Entry<V> entry) {
if (entry != null) {
final Instant now = now();
final long now = now();
entry.touch(now);
}
}
private boolean isExpired(final Entry<V> entry, final Instant now) {
return entry.getLastAccessed().plus(timeToLive).isBefore(now);
private boolean isExpired(final Entry<V> entry, final long now) {
return (entry.getLastAccessed() + timeToLive.toMillis()) < now;
}
private void handleEvent(final K key, final V value) {