[Change] CacheStore-local, CacheStore-redis 适配 CacheStore-API 模块的更改;

[Change] LocalCacheStoreFactory 适配更改;
[Change] RedisCacheStoreFactory 适配更改, 增加 Redis 配置文件相关逻辑;
[Change] RedisConnectionPool 增加 Redis 连接配置功能;
This commit is contained in:
LamGC 2020-11-05 00:55:40 +08:00
parent b7d712da21
commit 4a2337afd7
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
3 changed files with 76 additions and 1 deletions

View File

@ -20,6 +20,8 @@ package net.lamgc.cgj.bot.cache.local;
import net.lamgc.cgj.bot.cache.*; import net.lamgc.cgj.bot.cache.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter; 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) @Factory(name = "Local-Memory", priority = FactoryPriority.PRIORITY_LOWEST, source = CacheStoreSource.MEMORY)
public class LocalCacheStoreFactory implements CacheStoreFactory { public class LocalCacheStoreFactory implements CacheStoreFactory {
@Override
public void initial(File dataDirectory) {
// 不需要做任何事情, 除非需要做持久化.
}
@Override @Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) { public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) {
return new HashSingleCacheStore<>(); return new HashSingleCacheStore<>();

View File

@ -17,9 +17,18 @@
package net.lamgc.cgj.bot.cache.redis; 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.*;
import net.lamgc.cgj.bot.cache.convert.StringConverter; import net.lamgc.cgj.bot.cache.convert.StringConverter;
import net.lamgc.cgj.bot.cache.exception.GetCacheStoreException; 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) @Factory(name = "Redis", priority = FactoryPriority.PRIORITY_HIGHER, source = CacheStoreSource.REMOTE)
public class RedisCacheStoreFactory implements CacheStoreFactory { 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 @Override
public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) { public <V> SingleCacheStore<V> newSingleCacheStore(String identify, StringConverter<V> converter) {
return new RedisSingleCacheStore<>(identify, converter); return new RedisSingleCacheStore<>(identify, converter);

View File

@ -23,6 +23,7 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
import java.net.URL;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function; import java.util.function.Function;
@ -34,6 +35,13 @@ class RedisConnectionPool {
private final static Logger log = LoggerFactory.getLogger(RedisConnectionPool.class); private final static Logger log = LoggerFactory.getLogger(RedisConnectionPool.class);
private final static AtomicReference<JedisPool> POOL = new AtomicReference<>(); private final static AtomicReference<JedisPool> POOL = new AtomicReference<>();
private final static AtomicReference<URL> 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() { public static synchronized void reconnectRedis() {
JedisPool jedisPool = POOL.get(); JedisPool jedisPool = POOL.get();
@ -43,7 +51,13 @@ class RedisConnectionPool {
JedisPoolConfig config = new JedisPoolConfig(); JedisPoolConfig config = new JedisPoolConfig();
config.setTestOnBorrow(true); config.setTestOnBorrow(true);
config.setTestOnReturn(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); POOL.set(jedisPool);
} }