From 02f6574c09d228837c2a8dbe65f1f9201c446801 Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 16 Aug 2024 16:48:54 +0800 Subject: [PATCH] fix[cache]: fixed a bug where expired data could not be deleted --- .../com/zfoo/scheduler/util/LazyCache.java | 7 ++--- .../zfoo/scheduler/util/LazyCacheTesting.java | 30 +++++++++---------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java b/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java index 3858ffdb..2df52f80 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java @@ -85,9 +85,6 @@ public class LazyCache { var cacheValue = new CacheValue(); cacheValue.value = value; cacheValue.expireTime = TimeUtils.now() + expireAfterAccessMillis; - if (cacheValue.expireTime < this.minExpireTime) { - this.minExpireTime = cacheValue.expireTime; - } var oldCacheValue = cacheMap.put(key, cacheValue); if (oldCacheValue != null) { removeListener.accept(List.of(new Pair<>(key, oldCacheValue.value)), RemovalCause.REPLACED); @@ -146,6 +143,7 @@ public class LazyCache { .limit(Math.max(0, cacheMap.size() - maximumSize)) .map(it -> new Pair<>(it.getKey(), it.getValue().value)) .toList(); + removeList.forEach(it -> cacheMap.remove(it.getKey())); removeListener.accept(removeList, RemovalCause.SIZE); } } @@ -168,8 +166,9 @@ public class LazyCache { minTimestamp = expireTime; } } + removeList.forEach(it -> cacheMap.remove(it.getKey())); removeListener.accept(removeList, RemovalCause.EXPIRED); - if (this.minExpireTime < Long.MAX_VALUE) { + if (minTimestamp < Long.MAX_VALUE) { this.minExpireTime = minTimestamp; } } diff --git a/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTesting.java b/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTesting.java index 9b2baae9..4f964a0c 100644 --- a/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTesting.java +++ b/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTesting.java @@ -45,28 +45,26 @@ public class LazyCacheTesting { lazyCache.put(10, "j"); lazyCache.put(11, "k"); lazyCache.put(12, "l"); - ThreadUtils.sleep(1000); + ThreadUtils.sleep(3000); lazyCache.put(13, "m"); - ThreadUtils.sleep(1000); + ThreadUtils.sleep(3000); lazyCache.put(14, "n"); - ThreadUtils.sleep(1000); + ThreadUtils.sleep(3000); } @Test public void expireTest() { - var lazyCache = new LazyCache(10, 10 * TimeUtils.MILLIS_PER_SECOND, 5 * TimeUtils.MILLIS_PER_SECOND, myRemoveCallback); + var lazyCache = new LazyCache(10, 3 * TimeUtils.MILLIS_PER_SECOND, 1 * TimeUtils.MILLIS_PER_SECOND, myRemoveCallback); - lazyCache.put(1, "a"); - lazyCache.put(2, "b"); - lazyCache.put(3, "c"); - lazyCache.put(4, "d"); - lazyCache.put(5, "e"); - ThreadUtils.sleep(11 * TimeUtils.MILLIS_PER_SECOND); - System.out.println(lazyCache.get(1)); - System.out.println(lazyCache.get(2)); - System.out.println(lazyCache.get(3)); - System.out.println(lazyCache.get(4)); - System.out.println(lazyCache.get(5)); + for (int i = 0; i < 10; i++) { + lazyCache.put(1, "a"); + lazyCache.put(2, "b"); + lazyCache.put(3, "c"); + lazyCache.put(4, "d"); + lazyCache.put(5, "e"); + ThreadUtils.sleep(5 * TimeUtils.MILLIS_PER_SECOND); + System.out.println(lazyCache.get(1)); + } } @Test @@ -78,7 +76,7 @@ public class LazyCacheTesting { lazyCache.put(3, "c"); lazyCache.put(4, "d"); lazyCache.put(5, "e"); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < 12; i++) { lazyCache.get(1); lazyCache.get(2); ThreadUtils.sleep(1 * TimeUtils.MILLIS_PER_SECOND);