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 db63ff5..d05428e 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java @@ -27,6 +27,15 @@ public interface CacheStore { */ T getCache(String key); + /** + * 针对列表缓存的获取 + * @param key 键名 + * @param index 起始索引 + * @param length 选取长度 + * @return 返回指定类型, 如不支持, 该方法的行为应与{@linkplain #getCache(String) getCache(String)}一致 + */ + T getCache(String key, long index, long length); + /** * 如果该键存在且未过期则返回true. * @param key 要查询的键 @@ -42,6 +51,13 @@ public interface CacheStore { */ boolean exists(String key, Date date); + /** + * 查询列表缓存的长度 + * @param key 键名 + * @return 返回指定类型, 如不支持或不存在, 将返回-1 + */ + long length(String key); + /** * 清空缓存 * @return 如果清空成功, 返回true @@ -54,4 +70,11 @@ public interface CacheStore { */ boolean supportedPersistence(); + /** + * 是否支持列表缓存. + * 当本方法返回true时, {@link #length(String)}和{@link #getCache(String, long, long)}必须有具体实现而不能做兼容性处理. + * @return 如果支持返回true + */ + boolean supportedList(); + } 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 ca8f0de..967c210 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -56,6 +56,11 @@ public class LocalHashCacheStore implements CacheStore { return cacheObject.get(); } + @Override + public T getCache(String key, long index, long length) { + return getCache(key); + } + @Override public boolean exists(String key) { return exists(key, null); @@ -74,6 +79,11 @@ public class LocalHashCacheStore implements CacheStore { return true; } + @Override + public long length(String key) { + return -1; + } + @Override public boolean clear() { cache.clear(); @@ -84,4 +94,9 @@ public class LocalHashCacheStore implements CacheStore { public boolean supportedPersistence() { return false; } + + @Override + public boolean supportedList() { + 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 906c057..7c2eb3e 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java @@ -9,6 +9,8 @@ import redis.clients.jedis.*; import java.net.URI; import java.util.Date; import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Function; public abstract class RedisPoolCacheStore implements CacheStore { @@ -106,6 +108,43 @@ public abstract class RedisPoolCacheStore implements CacheStore { return true; } + /** + * 执行Jedis相关操作. + * @param consumer 执行方法 + */ + protected void executeJedisCommand(Consumer consumer) { + try (Jedis jedis = this.jedisPool.getResource()) { + consumer.accept(jedis); + } + } + + /** + * 执行Jedis相关操作. + * @param function 执行方法 + * @param 返回值类型 + * @return 返回提供Function函数式接口所返回的东西 + */ + protected R executeJedisCommand(Function function) { + try (Jedis jedis = this.jedisPool.getResource()) { + return function.apply(jedis); + } + } + + @Override + public T getCache(String key, long index, long length) { + return getCache(key); + } + + @Override + public long length(String key) { + return -1; + } + + @Override + public boolean supportedList() { + return false; + } + /** * 替换原本的分隔符(.)为(:).
* 即将启用