use only one thread for evictions
Instead of spawning a new thread for every cache, we use a single thread that will evict entries from all caches. The thread keeps a weak reference to the caches, so that they can be garbage collected.
This commit is contained in:
@@ -37,11 +37,12 @@ public class HotEntryCacheTest {
|
||||
public void testEvictOnGet() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock();
|
||||
final Duration timeToLive = Duration.ofSeconds(10);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock, 1, TimeUnit.MILLISECONDS);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock);
|
||||
|
||||
cache.put("key", "value1");
|
||||
|
||||
clock.plus(timeToLive.plusMillis(1));
|
||||
cache.triggerEviction();
|
||||
|
||||
final String cachedValue1_evicted = cache.get("key");
|
||||
Assert.assertEquals(cachedValue1_evicted, null);
|
||||
@@ -50,7 +51,7 @@ public class HotEntryCacheTest {
|
||||
public void testEvictionByBackgroundThread() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock();
|
||||
final Duration timeToLive = Duration.ofSeconds(10);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock, 1, TimeUnit.MILLISECONDS);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock);
|
||||
|
||||
final CompletableFuture<String> evictionEventFuture = new CompletableFuture<>();
|
||||
cache.addListener(event -> {
|
||||
@@ -60,6 +61,7 @@ public class HotEntryCacheTest {
|
||||
cache.put("key", "value1");
|
||||
|
||||
clock.plus(timeToLive.plusMillis(1));
|
||||
cache.triggerEviction();
|
||||
|
||||
final String evictedValue1 = evictionEventFuture.get(5, TimeUnit.MINUTES); // enough time for debugging
|
||||
Assert.assertEquals(evictedValue1, "value1");
|
||||
@@ -101,7 +103,7 @@ public class HotEntryCacheTest {
|
||||
public void testForEachTouches() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock();
|
||||
final Duration timeToLive = Duration.ofSeconds(10);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock, 1, TimeUnit.HOURS);
|
||||
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock);
|
||||
|
||||
final CompletableFuture<String> evictionEventFuture = new CompletableFuture<>();
|
||||
cache.addListener(event -> {
|
||||
|
||||
Reference in New Issue
Block a user