From 8375b81b17228b9ccb46bba35a0e21533b921acb Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 10 Apr 2020 22:00:38 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20=E8=B0=83=E6=95=B4=E5=8C=85=E8=B7=AF?= =?UTF-8?q?=E5=BE=84;=20[Add]=20RedisPoolCacheStore,=20JsonRedisCacheStore?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E5=8F=AF=E6=8F=90=E4=BE=9BJedisPool?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95?= =?UTF-8?q?,=20=E4=BB=A5=E5=AE=9E=E7=8E=B0Redis=E8=BF=9E=E6=8E=A5=E6=B1=A0?= =?UTF-8?q?=E5=85=B1=E7=94=A8;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cgj/{ => bot}/cache/CacheObject.java | 2 +- .../lamgc/cgj/{ => bot}/cache/CacheStore.java | 2 +- .../{ => bot}/cache/ImageCacheHandler.java | 2 +- .../cgj/{ => bot}/cache/ImageCacheObject.java | 2 +- .../{ => bot}/cache/JsonRedisCacheStore.java | 8 +++++++- .../{ => bot}/cache/LocalHashCacheStore.java | 2 +- .../cgj/{ => bot}/cache/RedisCacheStore.java | 7 ++++--- .../{ => bot}/cache/RedisPoolCacheStore.java | 20 ++++++++++++++++--- 8 files changed, 33 insertions(+), 12 deletions(-) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/CacheObject.java (96%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/CacheStore.java (97%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/ImageCacheHandler.java (98%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/ImageCacheObject.java (97%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/JsonRedisCacheStore.java (73%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/LocalHashCacheStore.java (98%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/RedisCacheStore.java (91%) rename src/main/java/net/lamgc/cgj/{ => bot}/cache/RedisPoolCacheStore.java (77%) diff --git a/src/main/java/net/lamgc/cgj/cache/CacheObject.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java similarity index 96% rename from src/main/java/net/lamgc/cgj/cache/CacheObject.java rename to src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java index dc7b49a..281d95d 100644 --- a/src/main/java/net/lamgc/cgj/cache/CacheObject.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import java.util.Date; import java.util.concurrent.atomic.AtomicReference; diff --git a/src/main/java/net/lamgc/cgj/cache/CacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java similarity index 97% rename from src/main/java/net/lamgc/cgj/cache/CacheStore.java rename to src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java index 7f7e98c..9060b85 100644 --- a/src/main/java/net/lamgc/cgj/cache/CacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/CacheStore.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import java.util.Date; diff --git a/src/main/java/net/lamgc/cgj/cache/ImageCacheHandler.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java similarity index 98% rename from src/main/java/net/lamgc/cgj/cache/ImageCacheHandler.java rename to src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java index a19b005..5ed0147 100644 --- a/src/main/java/net/lamgc/cgj/cache/ImageCacheHandler.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheHandler.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import net.lamgc.cgj.Main; import net.lamgc.cgj.pixiv.PixivURL; diff --git a/src/main/java/net/lamgc/cgj/cache/ImageCacheObject.java b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheObject.java similarity index 97% rename from src/main/java/net/lamgc/cgj/cache/ImageCacheObject.java rename to src/main/java/net/lamgc/cgj/bot/cache/ImageCacheObject.java index 75187f4..f3ca384 100644 --- a/src/main/java/net/lamgc/cgj/cache/ImageCacheObject.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/ImageCacheObject.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import net.lamgc.utils.event.EventObject; diff --git a/src/main/java/net/lamgc/cgj/cache/JsonRedisCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/JsonRedisCacheStore.java similarity index 73% rename from src/main/java/net/lamgc/cgj/cache/JsonRedisCacheStore.java rename to src/main/java/net/lamgc/cgj/bot/cache/JsonRedisCacheStore.java index 0e15c4d..76467cd 100644 --- a/src/main/java/net/lamgc/cgj/cache/JsonRedisCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/JsonRedisCacheStore.java @@ -1,7 +1,8 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import com.google.gson.Gson; import com.google.gson.JsonElement; +import redis.clients.jedis.JedisPool; import java.net.URI; @@ -14,6 +15,11 @@ public class JsonRedisCacheStore extends RedisPoolCacheStore { this.gson = gson; } + public JsonRedisCacheStore(JedisPool jedisPool, String prefix, Gson gson) { + super(jedisPool, prefix); + this.gson = gson; + } + @Override protected String parse(JsonElement data) { return this.gson.toJson(data); diff --git a/src/main/java/net/lamgc/cgj/cache/LocalHashCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java similarity index 98% rename from src/main/java/net/lamgc/cgj/cache/LocalHashCacheStore.java rename to src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java index fcc59a7..8830a5f 100644 --- a/src/main/java/net/lamgc/cgj/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -1,4 +1,4 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; import java.util.Date; import java.util.Hashtable; diff --git a/src/main/java/net/lamgc/cgj/cache/RedisCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/RedisCacheStore.java similarity index 91% rename from src/main/java/net/lamgc/cgj/cache/RedisCacheStore.java rename to src/main/java/net/lamgc/cgj/bot/cache/RedisCacheStore.java index 5094b78..35fdf9b 100644 --- a/src/main/java/net/lamgc/cgj/cache/RedisCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/RedisCacheStore.java @@ -1,5 +1,6 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; +import com.google.common.base.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; @@ -27,12 +28,12 @@ public abstract class RedisCacheStore implements CacheStore { */ public RedisCacheStore(URI redisServerUri, String password, String prefix) throws JedisConnectionException { this.jedis = new Jedis(redisServerUri.getHost(), redisServerUri.getPort() <= 0 ? 6379 : redisServerUri.getPort()); - log = LoggerFactory.getLogger("RedisCacheDatabase@" + Integer.toHexString(jedis.hashCode())); + log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(jedis.hashCode())); log.info("Redis数据库连接状态: {}", jedis.ping()); if(password != null) { this.jedis.auth(password); } - if(prefix != null) { + if(!Strings.isNullOrEmpty(prefix)) { keyPrefix = prefix.endsWith(".") ? prefix : prefix + "."; } else { keyPrefix = ""; diff --git a/src/main/java/net/lamgc/cgj/cache/RedisPoolCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java similarity index 77% rename from src/main/java/net/lamgc/cgj/cache/RedisPoolCacheStore.java rename to src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java index 3800ce7..1d5bcf6 100644 --- a/src/main/java/net/lamgc/cgj/cache/RedisPoolCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/RedisPoolCacheStore.java @@ -1,5 +1,6 @@ -package net.lamgc.cgj.cache; +package net.lamgc.cgj.bot.cache; +import com.google.common.base.Strings; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -7,7 +8,7 @@ import redis.clients.jedis.*; import java.net.URI; import java.util.Date; -import java.util.List; +import java.util.Objects; public abstract class RedisPoolCacheStore implements CacheStore { @@ -23,7 +24,7 @@ public abstract class RedisPoolCacheStore implements CacheStore { jedisPool = new JedisPool(config == null ? new GenericObjectPoolConfig() : config, redisServerUri.getHost(), redisServerUri.getPort() <= 0 ? 6379 : redisServerUri.getPort(), timeout <= 0 ? Protocol.DEFAULT_TIMEOUT : timeout, password); - log = LoggerFactory.getLogger("RedisPoolCacheStore@" + Integer.toHexString(jedisPool.hashCode())); + log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(jedisPool.hashCode())); if(prefix != null) { keyPrefix = prefix.endsWith(".") ? prefix : prefix + "."; } else { @@ -31,6 +32,19 @@ public abstract class RedisPoolCacheStore implements CacheStore { } } + public RedisPoolCacheStore(JedisPool pool, String keyPrefix) { + jedisPool = Objects.requireNonNull(pool); + if(jedisPool.isClosed()) { + throw new IllegalStateException("JedisPool is closed"); + } + log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(jedisPool.hashCode())); + if(!Strings.isNullOrEmpty(keyPrefix)) { + this.keyPrefix = keyPrefix.endsWith(".") ? keyPrefix : keyPrefix + "."; + } else { + this.keyPrefix = ""; + } + } + @Override public void update(String key, T value, Date expire) { Jedis jedis = jedisPool.getResource();