mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Add] 增加Redis List的支持, 可通过List存储缓存项;
[Add] 增加支持JsonObject类型的RedisListCacheStore;
This commit is contained in:
parent
66d3d0aa31
commit
c0c9a2d299
37
src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java
vendored
Normal file
37
src/main/java/net/lamgc/cgj/bot/cache/JsonObjectRedisListCacheStore.java
vendored
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class JsonObjectRedisListCacheStore extends RedisListCacheStore<JsonObject> {
|
||||||
|
private final Gson gson;
|
||||||
|
|
||||||
|
public JsonObjectRedisListCacheStore(URI redisServerUri, String prefix, Gson gson) {
|
||||||
|
super(redisServerUri, prefix);
|
||||||
|
this.gson = gson;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObjectRedisListCacheStore(URI redisServerUri, JedisPoolConfig config, int timeout, String password, String prefix, Gson gson) {
|
||||||
|
super(redisServerUri, config, timeout, password, prefix);
|
||||||
|
this.gson = gson;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObjectRedisListCacheStore(JedisPool pool, String keyPrefix, Gson gson) {
|
||||||
|
super(pool, keyPrefix);
|
||||||
|
this.gson = gson;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String parseData(JsonObject dataObj) {
|
||||||
|
return gson.toJson(dataObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonObject analysisData(String str) {
|
||||||
|
return gson.fromJson(str, JsonObject.class);
|
||||||
|
}
|
||||||
|
}
|
94
src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java
vendored
Normal file
94
src/main/java/net/lamgc/cgj/bot/cache/RedisListCacheStore.java
vendored
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class RedisListCacheStore<T> extends RedisPoolCacheStore<List<T>> {
|
||||||
|
|
||||||
|
private final String keyPrefix;
|
||||||
|
|
||||||
|
public RedisListCacheStore(URI redisServerUri, String prefix) {
|
||||||
|
super(redisServerUri, prefix);
|
||||||
|
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisListCacheStore(URI redisServerUri, JedisPoolConfig config, int timeout, String password, String prefix) {
|
||||||
|
super(redisServerUri, config, timeout, password, prefix);
|
||||||
|
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisListCacheStore(JedisPool pool, String prefix) {
|
||||||
|
super(pool, prefix);
|
||||||
|
keyPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(String key, List<T> value, Date expire) {
|
||||||
|
executeJedisCommand(jedis -> {
|
||||||
|
String[] arr = new String[value.size()];
|
||||||
|
for (int i = 0; i < value.size(); i++) {
|
||||||
|
arr[i] = parseData(value.get(i));
|
||||||
|
}
|
||||||
|
jedis.rpush(keyPrefix + key, arr);
|
||||||
|
if(expire != null) {
|
||||||
|
jedis.pexpireAt(keyPrefix + key, expire.getTime());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getCache(String key) {
|
||||||
|
return getCache(key, 0, length(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getCache(String key, long index, long length) {
|
||||||
|
return executeJedisCommand(jedis -> {
|
||||||
|
List<String> strings = jedis.lrange(keyPrefix + key, Math.max(0, index), Math.max(0, index + length - 1));
|
||||||
|
List<T> results = new ArrayList<>(strings.size());
|
||||||
|
strings.forEach(item -> results.add(analysisData(item)));
|
||||||
|
return results;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long length(String key) {
|
||||||
|
return executeJedisCommand(jedis -> {
|
||||||
|
return jedis.llen(keyPrefix + key);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String parse(List<T> dataObj) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<T> analysis(String dataStr) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将数据转换成String
|
||||||
|
* @param dataObj 数据对象
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
public abstract String parseData(T dataObj);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将String转换成数据
|
||||||
|
* @param str String对象
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
public abstract T analysisData(String str);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supportedList() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user