From d676bc497264e6f6f7b2d91bc20ad95935e56ace Mon Sep 17 00:00:00 2001 From: godotg Date: Fri, 16 Aug 2024 18:19:05 +0800 Subject: [PATCH] perf[cache]: check expire cache in all access method --- .../com/zfoo/scheduler/util/LazyCache.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 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 b85a119a2..9201d7f4b 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java @@ -111,9 +111,24 @@ public V get(K key) { public void remove(K key) { + checkExpire(); removeForCause(key, RemovalCause.EXPLICIT); } + public void forEach(BiConsumer biConsumer) { + checkExpire(); + for (var entry : cacheMap.entrySet()) { + biConsumer.accept(entry.getKey(), entry.getValue().value); + } + } + + public int size() { + checkExpire(); + return cacheMap.size(); + } + + + // ----------------------------------------------------------------------------------------------------------------- private void removeForCause(K key, RemovalCause removalCause) { if (key == null) { return; @@ -134,18 +149,6 @@ private void removeForCause(List> list, RemovalCause removalCause) { removeListener.accept(removeList, removalCause); } - public void forEach(BiConsumer biConsumer) { - for (var entry : cacheMap.entrySet()) { - biConsumer.accept(entry.getKey(), entry.getValue().value); - } - } - - public int size() { - return cacheMap.size(); - } - - - // ----------------------------------------------------------------------------------------------------------------- private void checkMaximumSize() { if (cacheMap.size() > backPressureSize) { var removeList = cacheMap.entrySet()