From 6443ba68ab7a23b29d4e59e0ff91b8a31d2706bd Mon Sep 17 00:00:00 2001 From: LamGC Date: Tue, 30 Jun 2020 01:21:17 +0800 Subject: [PATCH] =?UTF-8?q?[Fix]=20AutoCleanTimer=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B8=85=E9=99=A4=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E9=87=8D=E5=A4=8D=E5=B7=A5=E4=BD=9C;=20[Fix]?= =?UTF-8?q?=20LocalHashCacheStore=20=E4=BF=AE=E5=A4=8Dclean=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=8A=9B=E5=87=BA'ConcurrentModificationException'?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98;=20[Change]=20Cac?= =?UTF-8?q?heStoreCentral=20=E8=B0=83=E6=95=B4PreLoadData=E7=9A=84?= =?UTF-8?q?=E7=83=AD=E7=82=B9=E7=BC=93=E5=AD=98=E6=97=B6=E9=97=B4;=20[Chan?= =?UTF-8?q?ge]=20HotDataCacheStore=20=E5=B0=86=E6=9C=80=E8=BF=91=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9A=84Key=E9=87=8D=E7=BD=AE=E5=85=B6=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4,=20=E8=B0=83=E6=95=B4clean=E8=BF=87?= =?UTF-8?q?=E7=A8=8B;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/cache/AutoCleanTimer.java | 2 +- .../lamgc/cgj/bot/cache/CacheStoreCentral.java | 3 ++- .../lamgc/cgj/bot/cache/HotDataCacheStore.java | 15 +++++++++++---- .../lamgc/cgj/bot/cache/LocalHashCacheStore.java | 10 +++++----- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java b/src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java index ee67552..d7db9d4 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/AutoCleanTimer.java @@ -18,7 +18,7 @@ public class AutoCleanTimer extends TimerTask { private final static Logger log = LoggerFactory.getLogger(AutoCleanTimer.class); static { - cleanTimer.schedule(new AutoCleanTimer(), 100L); + cleanTimer.schedule(new AutoCleanTimer(), 100L, 100L); } /** diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java index a6bbe93..5c61f8a 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStoreCentral.java @@ -84,7 +84,8 @@ public final class CacheStoreCentral { CacheStoreUtils.hashLocalHotDataStore( new JsonRedisCacheStore(BotGlobal.getGlobal().getRedisServer(), "illustPreLoadData", BotGlobal.getGlobal().getGson()), - 1800000, 900000); + // 600000, 120000); + 60000, 1); /** * 搜索内容缓存, 有效期 2 小时 */ diff --git a/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java index 51b5a94..d5dc84d 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java @@ -75,6 +75,9 @@ public class HotDataCacheStore implements CacheStore, Cleanable { log.trace("Current缓存库更新完成."); result = parentResult; } else { + // 更新该Key的过期时间 + current.update(key, result, + expireTime + (expireFloatRange <= 0 ? 0 : random.nextInt(expireFloatRange))); log.trace("Current缓存库缓存命中."); } return result; @@ -138,10 +141,14 @@ public class HotDataCacheStore implements CacheStore, Cleanable { *

该方法仅清理Current缓存库, 不会对上游缓存库造成影响.

*/ @Override - public void clean() { - for(String key : this.current.keys()) { - if(current.exists(key)) { - current.remove(key); + public void clean() throws Exception { + if(current instanceof Cleanable) { + ((Cleanable) current).clean(); + } else { + for(String key : this.current.keys()) { + if (!current.exists(key)) { + current.remove(key); + } } } } diff --git a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java index a5c75ef..4ef6196 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -2,10 +2,7 @@ package net.lamgc.cgj.bot.cache; import org.jetbrains.annotations.NotNull; -import java.util.Date; -import java.util.Hashtable; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.concurrent.atomic.AtomicReference; /** @@ -151,11 +148,14 @@ public class LocalHashCacheStore implements CacheStore, Cleanable { @Override public void clean() { Date currentDate = new Date(); + Set expireKeySet = new HashSet<>(); cache.forEach((key, value) -> { if(value.isExpire(currentDate)) { - cache.remove(key); + expireKeySet.add(key); } }); + + expireKeySet.forEach(cache::remove); } public static class CacheObject implements Comparable> {