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> {