From c9c8db7de9a7926e6ccb744f96efcca6d32e3b00 Mon Sep 17 00:00:00 2001 From: LamGC Date: Tue, 14 Apr 2020 10:37:07 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20CacheStore=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E5=88=97=E8=A1=A8=E7=BC=93=E5=AD=98=E7=9A=84=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=94=AF=E6=8C=81;=20[Update]=20LocalHashCacheStore,?= =?UTF-8?q?=20RedisPoolCacheStore=E5=A2=9E=E5=8A=A0=E5=AF=B9CacheStore?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9;=20[Add]=20RedisPoolCacheStore=20=E5=A2=9E=E5=8A=A0ex?= =?UTF-8?q?ecuteJedisCommand=E6=96=B9=E6=B3=95=E4=BB=A5=E5=90=91=E5=AD=90?= =?UTF-8?q?=E7=B1=BB=E6=8F=90=E4=BE=9B=E8=A6=86=E7=9B=96=E9=87=8D=E5=86=99?= =?UTF-8?q?=E9=87=8D=E7=82=B9=E6=96=B9=E6=B3=95=E7=9A=84=E8=83=BD=E5=8A=9B?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/cache/CacheStore.java | 23 +++++++++++ .../cgj/bot/cache/LocalHashCacheStore.java | 15 +++++++ .../cgj/bot/cache/RedisPoolCacheStore.java | 39 +++++++++++++++++++ 3 files changed, 77 insertions(+) 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; + } + /** * 替换原本的分隔符(.)为(:).
* 即将启用