diff --git a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java index 7cdd464..d657091 100644 --- a/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java +++ b/ContentGrabbingJi-CacheStore-local/src/main/java/net/lamgc/cgj/bot/cache/local/LocalCacheStoreFactory.java @@ -20,6 +20,8 @@ package net.lamgc.cgj.bot.cache.local; import net.lamgc.cgj.bot.cache.*; import net.lamgc.cgj.bot.cache.convert.StringConverter; +import java.io.File; + /** * 本地缓存存储容器工厂. * 最快速但又是最占内存的方法, 适用于远端缓存失效, 或无远端缓存的情况下使用. @@ -29,6 +31,11 @@ import net.lamgc.cgj.bot.cache.convert.StringConverter; @Factory(name = "Local-Memory", priority = FactoryPriority.PRIORITY_LOWEST, source = CacheStoreSource.MEMORY) public class LocalCacheStoreFactory implements CacheStoreFactory { + @Override + public void initial(File dataDirectory) { + // 不需要做任何事情, 除非需要做持久化. + } + @Override public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) { return new HashSingleCacheStore<>(); diff --git a/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisCacheStoreFactory.java b/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisCacheStoreFactory.java index 3020f23..c3a28f5 100644 --- a/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisCacheStoreFactory.java +++ b/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisCacheStoreFactory.java @@ -17,9 +17,18 @@ package net.lamgc.cgj.bot.cache.redis; +import com.google.common.base.Strings; import net.lamgc.cgj.bot.cache.*; import net.lamgc.cgj.bot.cache.convert.StringConverter; import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Properties; /** * @@ -27,6 +36,51 @@ import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; */ @Factory(name = "Redis", priority = FactoryPriority.PRIORITY_HIGHER, source = CacheStoreSource.REMOTE) public class RedisCacheStoreFactory implements CacheStoreFactory { + + private final static Logger log = LoggerFactory.getLogger(RedisCacheStoreFactory.class); + + private final static String PROP_HOST = "redis.host"; + private final static String PROP_PORT = "redis.port"; + private final static String PROP_USE_SSL = "redis.useSSL"; + private final static String PROP_USERNAME = "redis.username"; + private final static String PROP_PASSWORD = "redis.password"; + private final static String PROP_DATABASE = "redis.databaseId"; + private final static String PROP_CLIENT_NAME = "redis.clientName"; + + @Override + public void initial(File dataDirectory) { + final File propertiesFile = new File(dataDirectory, "redis.properties"); + if (!propertiesFile.exists()) { + log.warn("未找到 Redis 配置文件, 使用默认配置."); + return; + } else if (!propertiesFile.isFile()) { + log.warn("Redis 配置文件不是一个文件, 使用默认配置."); + return; + } + Properties properties = new Properties(); + try (Reader propertiesReader = new BufferedReader( + new InputStreamReader(new FileInputStream(propertiesFile), StandardCharsets.UTF_8))) { + properties.load(propertiesReader); + } catch (IOException e) { + log.error("读取 Redis 配置文件时发生异常", e); + } + try { + String queryString = "/?" + "ssl=" + properties.getProperty(PROP_USE_SSL, "false") + '&' + + "user=" + Strings.nullToEmpty(properties.getProperty(PROP_USERNAME)) + '&' + + "passwd=" + Strings.nullToEmpty(properties.getProperty(PROP_PASSWORD)) + '&' + + "database=" + properties.getProperty(PROP_DATABASE, "0") + '&' + + "clientName=" + Strings.nullToEmpty(properties.getProperty(PROP_CLIENT_NAME)); + URL url = new URL("redis", + properties.getProperty(PROP_HOST, "localhost"), + Integer.parseInt(properties.getProperty(PROP_PORT, "6379")), + queryString); + + RedisConnectionPool.setConnectionUrl(url); + } catch (MalformedURLException e) { + log.error("构造连接 URL 时发生异常", e); + } + } + @Override public SingleCacheStore newSingleCacheStore(String identify, StringConverter converter) { return new RedisSingleCacheStore<>(identify, converter); diff --git a/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisConnectionPool.java b/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisConnectionPool.java index 1ac0a64..be127b7 100644 --- a/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisConnectionPool.java +++ b/ContentGrabbingJi-CacheStore-redis/src/main/java/net/lamgc/cgj/bot/cache/redis/RedisConnectionPool.java @@ -23,6 +23,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; +import java.net.URL; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; @@ -34,6 +35,13 @@ class RedisConnectionPool { private final static Logger log = LoggerFactory.getLogger(RedisConnectionPool.class); private final static AtomicReference POOL = new AtomicReference<>(); + private final static AtomicReference CONNECTION_URL = new AtomicReference<>(); + + public static synchronized void setConnectionUrl(URL connectionUrl) { + if(CONNECTION_URL.get() != null) { + CONNECTION_URL.set(connectionUrl); + } + } public static synchronized void reconnectRedis() { JedisPool jedisPool = POOL.get(); @@ -43,7 +51,13 @@ class RedisConnectionPool { JedisPoolConfig config = new JedisPoolConfig(); config.setTestOnBorrow(true); config.setTestOnReturn(true); - jedisPool = new JedisPool(config); + URL connectionUrl = CONNECTION_URL.get(); + if (connectionUrl == null) { + jedisPool = new JedisPool(config); + } else { + jedisPool = new JedisPool(config, connectionUrl.getHost(), connectionUrl.getPort(), + connectionUrl.getPath().toLowerCase().contains("ssl=true")); + } POOL.set(jedisPool); }