mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Add] CacheStore 增加keys, remove方法;
[Update] HotDataCacheStore, LocalHashCacheStore, RedisPoolCacheStore 适配CacheStore的更改;
This commit is contained in:
parent
0f202cb076
commit
4afa414725
@ -1,6 +1,7 @@
|
|||||||
package net.lamgc.cgj.bot.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存库接口
|
* 缓存库接口
|
||||||
@ -68,6 +69,19 @@ public interface CacheStore<T> {
|
|||||||
*/
|
*/
|
||||||
boolean clear();
|
boolean clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取key集合
|
||||||
|
* @return 返回存储缓存库中所有缓存项key的集合
|
||||||
|
*/
|
||||||
|
Set<String> keys();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除指定缓存项
|
||||||
|
* @param key 缓存项key
|
||||||
|
* @return 删除成功返回true
|
||||||
|
*/
|
||||||
|
boolean remove(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否支持持久化
|
* 是否支持持久化
|
||||||
* @return 如果支持返回true
|
* @return 如果支持返回true
|
||||||
|
@ -3,9 +3,7 @@ package net.lamgc.cgj.bot.cache;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 具有继承性的热点数据缓存库
|
* 具有继承性的热点数据缓存库
|
||||||
@ -99,6 +97,21 @@ public class HotDataCacheStore<T> implements CacheStore<T> {
|
|||||||
return current.clear();
|
return current.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> keys() {
|
||||||
|
Set<String> keys = new HashSet<>();
|
||||||
|
keys.addAll(current.keys());
|
||||||
|
keys.addAll(parent.keys());
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(String key) {
|
||||||
|
parent.remove(key);
|
||||||
|
current.remove(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportedPersistence() {
|
public boolean supportedPersistence() {
|
||||||
return current.supportedPersistence() || parent.supportedPersistence();
|
return current.supportedPersistence() || parent.supportedPersistence();
|
||||||
|
@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,6 +98,16 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> keys() {
|
||||||
|
return cache.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(String key) {
|
||||||
|
return cache.remove(key) != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportedPersistence() {
|
public boolean supportedPersistence() {
|
||||||
return false;
|
return false;
|
||||||
|
@ -88,12 +88,20 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Set<String> keys() {
|
public Set<String> keys() {
|
||||||
try (Jedis jedis = jedisPool.getResource()) {
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
return jedis.keys(keyPrefix + "*");
|
return jedis.keys(keyPrefix + "*");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(String key) {
|
||||||
|
try (Jedis jedis = jedisPool.getResource()) {
|
||||||
|
return jedis.del(keyPrefix + key) == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换方法
|
* 转换方法
|
||||||
* @param dataObj 原数据
|
* @param dataObj 原数据
|
||||||
|
Loading…
Reference in New Issue
Block a user