From a4fe83de9a1c01997dce40a3c6871290124184fd Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 20 Aug 2024 14:22:47 +0800 Subject: [PATCH] perf[cache]: reduce copy objects --- .../src/main/java/com/zfoo/scheduler/util/LazyCache.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 41cf270b..5cab2f3b 100644 --- a/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java +++ b/scheduler/src/main/java/com/zfoo/scheduler/util/LazyCache.java @@ -84,7 +84,6 @@ public class LazyCache { */ public void put(K key, V value) { checkExpire(); - checkMaximumSize(); var cache = new Cache(); cache.k = key; @@ -94,6 +93,8 @@ public class LazyCache { if (oldCache != null) { removeListener.accept(List.of(oldCache), RemovalCause.REPLACED); } + + checkMaximumSize(); } public V get(K key) { @@ -114,11 +115,13 @@ public class LazyCache { public void remove(K key) { checkExpire(); + removeForCause(key, RemovalCause.EXPLICIT); } public void forEach(BiConsumer biConsumer) { checkExpire(); + for (var cache : cacheMap.values()) { biConsumer.accept(cache.k, cache.v); } @@ -126,6 +129,7 @@ public class LazyCache { public int size() { checkExpire(); + return cacheMap.size(); }