[Change] 调整包路径;

[Add] RedisPoolCacheStore, JsonRedisCacheStore 增加可提供JedisPool对象的构造方法, 以实现Redis连接池共用;
This commit is contained in:
LamGC 2020-04-10 22:00:38 +08:00
parent 9bf59da444
commit 8375b81b17
8 changed files with 33 additions and 12 deletions

View File

@ -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;

View File

@ -1,4 +1,4 @@
package net.lamgc.cgj.cache;
package net.lamgc.cgj.bot.cache;
import java.util.Date;

View File

@ -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;

View File

@ -1,4 +1,4 @@
package net.lamgc.cgj.cache;
package net.lamgc.cgj.bot.cache;
import net.lamgc.utils.event.EventObject;

View File

@ -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<JsonElement> {
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);

View File

@ -1,4 +1,4 @@
package net.lamgc.cgj.cache;
package net.lamgc.cgj.bot.cache;
import java.util.Date;
import java.util.Hashtable;

View File

@ -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<T> implements CacheStore<T> {
*/
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 = "";

View File

@ -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<T> implements CacheStore<T> {
@ -23,7 +24,7 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
jedisPool = new JedisPool(config == null ? new GenericObjectPoolConfig<JedisPool>() : 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<T> implements CacheStore<T> {
}
}
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();