From 03d79968152de1f7fbdc851ff69e3b4e83157b06 Mon Sep 17 00:00:00 2001 From: godotg Date: Wed, 27 Mar 2024 17:00:50 +0800 Subject: [PATCH] del[cache]: delete sizeCheckTimeAtomic --- .../com/zfoo/scheduler/util/LazyCache.java | 22 +++++-------------- .../zfoo/scheduler/util/LazyCacheTest.java | 5 ++++- 2 files changed, 10 insertions(+), 17 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 c5eab22c0..b1b9a3d69 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java @@ -17,7 +17,6 @@ public class LazyCache { private static final float DEFAULT_BACK_PRESSURE_FACTOR = 0.11f; - private static final long MILLIS_MAX_SIZE_CHECK_INTERVAL = 3; private static class CacheValue { public volatile V value; @@ -58,7 +57,6 @@ public static enum RemovalCause { private long expireCheckIntervalMillis; private volatile long minExpireTime; private AtomicLong expireCheckTimeAtomic; - private AtomicLong sizeCheckTimeAtomic; private ConcurrentMap> cacheMap; private BiConsumer, RemovalCause> removeListener = (pair, removalCause) -> { }; @@ -70,7 +68,6 @@ public LazyCache(int maximumSize, long expireAfterAccessMillis, long expireCheck this.expireCheckIntervalMillis = expireCheckIntervalMillis; this.minExpireTime = TimeUtils.now(); this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now() + expireCheckIntervalMillis); - this.sizeCheckTimeAtomic = new AtomicLong(TimeUtils.now()); this.cacheMap = new ConcurrentHashMap<>(Math.max(maximumSize / 16, 512)); if (removeListener != null) { this.removeListener = removeListener; @@ -136,19 +133,12 @@ public int size() { // ----------------------------------------------------------------------------------------------------------------- private void checkMaximumSize() { if (cacheMap.size() > backPressureSize) { - var currentTimeMillis = TimeUtils.currentTimeMillis(); - var sizeCheckTime = sizeCheckTimeAtomic.get(); - if (currentTimeMillis > sizeCheckTime) { - if (sizeCheckTimeAtomic.compareAndSet(sizeCheckTime, currentTimeMillis + MILLIS_MAX_SIZE_CHECK_INTERVAL)) { - var exceedList = cacheMap.entrySet() - .stream() - .map(it -> new PairLong<>(it.getValue().expireTime, it.getKey())) - .sorted((a, b) -> Long.compare(a.getKey(), b.getKey())) - .limit(Math.max(0, cacheMap.size() - maximumSize)) - .toList(); - exceedList.forEach(it -> removeForCause(it.getValue(), RemovalCause.SIZE)); - } - } + cacheMap.entrySet() + .stream() + .map(it -> new PairLong<>(it.getValue().expireTime, it.getKey())) + .sorted((a, b) -> Long.compare(a.getKey(), b.getKey())) + .limit(Math.max(0, cacheMap.size() - maximumSize)) + .forEach(it -> removeForCause(it.getValue(), RemovalCause.SIZE)); } } diff --git a/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTest.java b/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTest.java index da70575c5..fb2b5e86b 100644 --- a/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTest.java +++ b/scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTest.java @@ -127,7 +127,10 @@ public void run() { } }); } - ThreadUtils.sleep(Long.MAX_VALUE); + for (int i = 0; i < 10000; i++) { + logger.info("cache size:[{}]", lazyCache.size()); + ThreadUtils.sleep(1000); + } } }