From 4afa414725c40cd2d740c30cbbb0ea415144ff1d Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 24 Apr 2020 01:23:11 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20CacheStore=20=E5=A2=9E=E5=8A=A0keys,=20?= =?UTF-8?q?remove=E6=96=B9=E6=B3=95;=20[Update]=20HotDataCacheStore,=20Loc?= =?UTF-8?q?alHashCacheStore,=20RedisPoolCacheStore=20=E9=80=82=E9=85=8DCac?= =?UTF-8?q?heStore=E7=9A=84=E6=9B=B4=E6=94=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/cache/CacheStore.java | 14 ++++++++++++++ .../cgj/bot/cache/HotDataCacheStore.java | 19 ++++++++++++++++--- .../cgj/bot/cache/LocalHashCacheStore.java | 11 +++++++++++ .../cgj/bot/cache/RedisPoolCacheStore.java | 8 ++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java index 04bceef..98039df 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java @@ -1,6 +1,7 @@ package net.lamgc.cgj.bot.cache; import java.util.Date; +import java.util.Set; /** * 缓存库接口 @@ -68,6 +69,19 @@ public interface CacheStore { */ boolean clear(); + /** + * 获取key集合 + * @return 返回存储缓存库中所有缓存项key的集合 + */ + Set keys(); + + /** + * 删除指定缓存项 + * @param key 缓存项key + * @return 删除成功返回true + */ + boolean remove(String key); + /** * 是否支持持久化 * @return 如果支持返回true 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 d3d13aa..e2d24eb 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/HotDataCacheStore.java @@ -3,9 +3,7 @@ package net.lamgc.cgj.bot.cache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; -import java.util.Objects; -import java.util.Random; +import java.util.*; /** * 具有继承性的热点数据缓存库 @@ -99,6 +97,21 @@ public class HotDataCacheStore implements CacheStore { return current.clear(); } + @Override + public Set keys() { + Set keys = new HashSet<>(); + keys.addAll(current.keys()); + keys.addAll(parent.keys()); + return keys; + } + + @Override + public boolean remove(String key) { + parent.remove(key); + current.remove(key); + return true; + } + @Override public boolean supportedPersistence() { return current.supportedPersistence() || parent.supportedPersistence(); 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 3facf38..d89ffd0 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Date; import java.util.Hashtable; import java.util.Objects; +import java.util.Set; import java.util.concurrent.atomic.AtomicReference; /** @@ -97,6 +98,16 @@ public class LocalHashCacheStore implements CacheStore { return true; } + @Override + public Set keys() { + return cache.keySet(); + } + + @Override + public boolean remove(String key) { + return cache.remove(key) != null; + } + @Override public boolean supportedPersistence() { return false; diff --git a/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java index 8aef9a6..0645aaf 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java @@ -88,12 +88,20 @@ public abstract class RedisPoolCacheStore implements CacheStore { } } + @Override public Set keys() { try (Jedis jedis = jedisPool.getResource()) { return jedis.keys(keyPrefix + "*"); } } + @Override + public boolean remove(String key) { + try (Jedis jedis = jedisPool.getResource()) { + return jedis.del(keyPrefix + key) == 1; + } + } + /** * 转换方法 * @param dataObj 原数据