[Add] CacheStore 增加keys, remove方法;

[Update] HotDataCacheStore, LocalHashCacheStore, RedisPoolCacheStore 适配CacheStore的更改;
This commit is contained in:
LamGC 2020-04-24 01:23:11 +08:00
parent 0f202cb076
commit 4afa414725
4 changed files with 49 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package net.lamgc.cgj.bot.cache;
import java.util.Date;
import java.util.Set;
/**
* 缓存库接口
@ -68,6 +69,19 @@ public interface CacheStore<T> {
*/
boolean clear();
/**
* 获取key集合
* @return 返回存储缓存库中所有缓存项key的集合
*/
Set<String> keys();
/**
* 删除指定缓存项
* @param key 缓存项key
* @return 删除成功返回true
*/
boolean remove(String key);
/**
* 是否支持持久化
* @return 如果支持返回true

View File

@ -3,9 +3,7 @@ package net.lamgc.cgj.bot.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.Objects;
import java.util.Random;
import java.util.*;
/**
* 具有继承性的热点数据缓存库
@ -99,6 +97,21 @@ public class HotDataCacheStore<T> implements CacheStore<T> {
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
public boolean supportedPersistence() {
return current.supportedPersistence() || parent.supportedPersistence();

View File

@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Date;
import java.util.Hashtable;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
/**
@ -97,6 +98,16 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
return true;
}
@Override
public Set<String> keys() {
return cache.keySet();
}
@Override
public boolean remove(String key) {
return cache.remove(key) != null;
}
@Override
public boolean supportedPersistence() {
return false;

View File

@ -88,12 +88,20 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
}
}
@Override
public Set<String> keys() {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.keys(keyPrefix + "*");
}
}
@Override
public boolean remove(String key) {
try (Jedis jedis = jedisPool.getResource()) {
return jedis.del(keyPrefix + key) == 1;
}
}
/**
* 转换方法
* @param dataObj 原数据