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 原数据