From 1f402fbbac5ca9176e121015ff041684795986d3 Mon Sep 17 00:00:00 2001 From: LamGC Date: Wed, 20 May 2020 08:56:27 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20LocalHashCacheStore=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0'Cleanable'=E6=8E=A5=E5=8F=A3,=20=E5=A2=9E=E5=8A=A0'(int,=20int,=20boolean)'=E6=9E=84=E9=80=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=BB=A5=E5=85=81=E8=AE=B8=E8=87=AA=E5=8A=A8=E6=B8=85?= =?UTF-8?q?=E7=90=86=20[Update]=20LocalHashCacheStore=20=E8=A1=A5=E5=85=85?= =?UTF-8?q?Javadoc=E5=86=85=E5=AE=B9;=20[Update]=20.gitignore=20=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E5=B9=B6=E6=95=B4=E7=90=86=E5=BF=BD=E7=95=A5=E9=A1=B9?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 13 +++++- .../cgj/bot/cache/LocalHashCacheStore.java | 41 ++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index bf2955d..1bb7a6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,15 @@ +# Ignore test date folder /pluginData/ /logs/ -/.idea/ -/CGJ_2.iml /cookies.store /target/ + +# Ignore Idea files +/.idea/ +/CGJ_2.iml + +# Ignore Visual Studio Code files +.classpath +.factorypath +.project +/.settings/ 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 d89ffd0..d5c63b1 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -12,19 +12,45 @@ import java.util.concurrent.atomic.AtomicReference; * 基于Hashtable的本地缓存库 * @param 缓存类型 */ -public class LocalHashCacheStore implements CacheStore { +public class LocalHashCacheStore implements CacheStore, Cleanable { private final Hashtable> cache; + /** + * 构造一个基于Hashtable的本地缓存库 + * @see Hashtable + */ public LocalHashCacheStore() { this(0); } + /** + * 构造一个基于Hashtable的本地缓存库 + * @param initialCapacity 初始容量 + * @see Hashtable + */ public LocalHashCacheStore(int initialCapacity) { this(initialCapacity, 0F); } + /** + * 构造一个基于Hashtable的本地缓存库 + * @param initialCapacity 初始容量 + * @param loadFactor 重载因子 + * @see Hashtable + */ public LocalHashCacheStore(int initialCapacity, float loadFactor) { + this(initialCapacity, loadFactor, false); + } + + /** + * 构造一个基于Hashtable的本地缓存库 + * @param initialCapacity 初始容量 + * @param loadFactor 重载因子 + * @param autoClean 是否自动清理 + * @see Hashtable + */ + public LocalHashCacheStore(int initialCapacity, float loadFactor, boolean autoClean) { if(initialCapacity != 0) { if(loadFactor <= 0F) { cache = new Hashtable<>(initialCapacity); @@ -34,6 +60,10 @@ public class LocalHashCacheStore implements CacheStore { } else { cache = new Hashtable<>(); } + + if(autoClean) { + AutoCleanTimer.add(this); + } } @Override @@ -118,6 +148,15 @@ public class LocalHashCacheStore implements CacheStore { return false; } + @Override + public void clean() throws Exception { + Date currentDate = new Date(); + cache.forEach((key, value) -> { + if(value.isExpire(currentDate)) { + cache.remove(key); + } + }); + } public static class CacheObject implements Comparable> {