From c0c9a2d29926e9ac52edae84c4d55fc0c56ee7e4 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 16 Apr 2020 10:53:57 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20=E5=A2=9E=E5=8A=A0Redis=20List=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81,=20=E5=8F=AF=E9=80=9A=E8=BF=87List=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E7=BC=93=E5=AD=98=E9=A1=B9;=20[Add]=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=94=AF=E6=8C=81JsonObject=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84RedisListCacheStore;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/JsonObjectRedisListCacheStore.java | 37 ++++++++ .../cgj/bot/cache/RedisListCacheStore.java | 94 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java create mode 100644 src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java diff --git a/src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java new file mode 100644 index 0000000..65fb659 --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java @@ -0,0 +1,37 @@ +package net.lamgc.cgj.bot.cache; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +import java.net.URI; + +public class JsonObjectRedisListCacheStore extends RedisListCacheStore { + private final Gson gson; + + public JsonObjectRedisListCacheStore(URI redisServerUri, String prefix, Gson gson) { + super(redisServerUri, prefix); + this.gson = gson; + } + + public JsonObjectRedisListCacheStore(URI redisServerUri, JedisPoolConfig config, int timeout, String password, String prefix, Gson gson) { + super(redisServerUri, config, timeout, password, prefix); + this.gson = gson; + } + + public JsonObjectRedisListCacheStore(JedisPool pool, String keyPrefix, Gson gson) { + super(pool, keyPrefix); + this.gson = gson; + } + + @Override + public String parseData(JsonObject dataObj) { + return gson.toJson(dataObj); + } + + @Override + public JsonObject analysisData(String str) { + return gson.fromJson(str, JsonObject.class); + } +} diff --git a/src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java new file mode 100644 index 0000000..768288f --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java @@ -0,0 +1,94 @@ +package net.lamgc.cgj.bot.cache; + +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public abstract class RedisListCacheStore extends RedisPoolCacheStore> { + + private final String keyPrefix; + + public RedisListCacheStore(URI redisServerUri, String prefix) { + super(redisServerUri, prefix); + keyPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + } + + public RedisListCacheStore(URI redisServerUri, JedisPoolConfig config, int timeout, String password, String prefix) { + super(redisServerUri, config, timeout, password, prefix); + keyPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + } + + public RedisListCacheStore(JedisPool pool, String prefix) { + super(pool, prefix); + keyPrefix = prefix.endsWith(".") ? prefix : prefix + "."; + } + + @Override + public void update(String key, List value, Date expire) { + executeJedisCommand(jedis -> { + String[] arr = new String[value.size()]; + for (int i = 0; i < value.size(); i++) { + arr[i] = parseData(value.get(i)); + } + jedis.rpush(keyPrefix + key, arr); + if(expire != null) { + jedis.pexpireAt(keyPrefix + key, expire.getTime()); + } + }); + } + + @Override + public List getCache(String key) { + return getCache(key, 0, length(key)); + } + + @Override + public List getCache(String key, long index, long length) { + return executeJedisCommand(jedis -> { + List strings = jedis.lrange(keyPrefix + key, Math.max(0, index), Math.max(0, index + length - 1)); + List results = new ArrayList<>(strings.size()); + strings.forEach(item -> results.add(analysisData(item))); + return results; + }); + } + + @Override + public long length(String key) { + return executeJedisCommand(jedis -> { + return jedis.llen(keyPrefix + key); + }); + } + + @Override + protected String parse(List dataObj) { + return null; + } + + @Override + protected List analysis(String dataStr) { + return null; + } + + /** + * 将数据转换成String + * @param dataObj 数据对象 + * @return 转换结果 + */ + public abstract String parseData(T dataObj); + + /** + * 将String转换成数据 + * @param str String对象 + * @return 转换结果 + */ + public abstract T analysisData(String str); + + @Override + public boolean supportedList() { + return true; + } +}