mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Change] 调整包路径;
[Add] RedisPoolCacheStore, JsonRedisCacheStore 增加可提供JedisPool对象的构造方法, 以实现Redis连接池共用;
This commit is contained in:
parent
9bf59da444
commit
8375b81b17
@ -1,4 +1,4 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
@ -1,4 +1,4 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import net.lamgc.cgj.Main;
|
import net.lamgc.cgj.Main;
|
||||||
import net.lamgc.cgj.pixiv.PixivURL;
|
import net.lamgc.cgj.pixiv.PixivURL;
|
@ -1,4 +1,4 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import net.lamgc.utils.event.EventObject;
|
import net.lamgc.utils.event.EventObject;
|
||||||
|
|
@ -1,7 +1,8 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
@ -14,6 +15,11 @@ public class JsonRedisCacheStore extends RedisPoolCacheStore<JsonElement> {
|
|||||||
this.gson = gson;
|
this.gson = gson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonRedisCacheStore(JedisPool jedisPool, String prefix, Gson gson) {
|
||||||
|
super(jedisPool, prefix);
|
||||||
|
this.gson = gson;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String parse(JsonElement data) {
|
protected String parse(JsonElement data) {
|
||||||
return this.gson.toJson(data);
|
return this.gson.toJson(data);
|
@ -1,4 +1,4 @@
|
|||||||
package net.lamgc.cgj.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
@ -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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import redis.clients.jedis.Jedis;
|
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 {
|
public RedisCacheStore(URI redisServerUri, String password, String prefix) throws JedisConnectionException {
|
||||||
this.jedis = new Jedis(redisServerUri.getHost(), redisServerUri.getPort() <= 0 ? 6379 : redisServerUri.getPort());
|
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());
|
log.info("Redis数据库连接状态: {}", jedis.ping());
|
||||||
if(password != null) {
|
if(password != null) {
|
||||||
this.jedis.auth(password);
|
this.jedis.auth(password);
|
||||||
}
|
}
|
||||||
if(prefix != null) {
|
if(!Strings.isNullOrEmpty(prefix)) {
|
||||||
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
} else {
|
} else {
|
||||||
keyPrefix = "";
|
keyPrefix = "";
|
@ -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.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -7,7 +8,7 @@ import redis.clients.jedis.*;
|
|||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
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(),
|
jedisPool = new JedisPool(config == null ? new GenericObjectPoolConfig<JedisPool>() : config, redisServerUri.getHost(),
|
||||||
redisServerUri.getPort() <= 0 ? 6379 : redisServerUri.getPort(),
|
redisServerUri.getPort() <= 0 ? 6379 : redisServerUri.getPort(),
|
||||||
timeout <= 0 ? Protocol.DEFAULT_TIMEOUT : timeout, password);
|
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) {
|
if(prefix != null) {
|
||||||
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
} else {
|
} 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
|
@Override
|
||||||
public void update(String key, T value, Date expire) {
|
public void update(String key, T value, Date expire) {
|
||||||
Jedis jedis = jedisPool.getResource();
|
Jedis jedis = jedisPool.getResource();
|
Loading…
Reference in New Issue
Block a user