[Add] CacheStore 增加对列表缓存的部分支持;

[Update] LocalHashCacheStore, RedisPoolCacheStore增加对CacheStore新增加方法的兼容;
[Add] RedisPoolCacheStore 增加executeJedisCommand方法以向子类提供覆盖重写重点方法的能力;
This commit is contained in:
LamGC 2020-04-14 10:37:07 +08:00
parent e20f9d34c9
commit c9c8db7de9
3 changed files with 77 additions and 0 deletions

View File

@ -27,6 +27,15 @@ public interface CacheStore<T> {
*/ */
T getCache(String key); T getCache(String key);
/**
* 针对列表缓存的获取
* @param key 键名
* @param index 起始索引
* @param length 选取长度
* @return 返回指定类型, 如不支持, 该方法的行为应与{@linkplain #getCache(String) getCache(String)}一致
*/
T getCache(String key, long index, long length);
/** /**
* 如果该键存在且未过期则返回true. * 如果该键存在且未过期则返回true.
* @param key 要查询的键 * @param key 要查询的键
@ -42,6 +51,13 @@ public interface CacheStore<T> {
*/ */
boolean exists(String key, Date date); boolean exists(String key, Date date);
/**
* 查询列表缓存的长度
* @param key 键名
* @return 返回指定类型, 如不支持或不存在, 将返回-1
*/
long length(String key);
/** /**
* 清空缓存 * 清空缓存
* @return 如果清空成功, 返回true * @return 如果清空成功, 返回true
@ -54,4 +70,11 @@ public interface CacheStore<T> {
*/ */
boolean supportedPersistence(); boolean supportedPersistence();
/**
* 是否支持列表缓存.
* 当本方法返回true时, {@link #length(String)}{@link #getCache(String, long, long)}必须有具体实现而不能做兼容性处理.
* @return 如果支持返回true
*/
boolean supportedList();
} }

View File

@ -56,6 +56,11 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
return cacheObject.get(); return cacheObject.get();
} }
@Override
public T getCache(String key, long index, long length) {
return getCache(key);
}
@Override @Override
public boolean exists(String key) { public boolean exists(String key) {
return exists(key, null); return exists(key, null);
@ -74,6 +79,11 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
return true; return true;
} }
@Override
public long length(String key) {
return -1;
}
@Override @Override
public boolean clear() { public boolean clear() {
cache.clear(); cache.clear();
@ -84,4 +94,9 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
public boolean supportedPersistence() { public boolean supportedPersistence() {
return false; return false;
} }
@Override
public boolean supportedList() {
return false;
}
} }

View File

@ -9,6 +9,8 @@ import redis.clients.jedis.*;
import java.net.URI; import java.net.URI;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class RedisPoolCacheStore<T> implements CacheStore<T> { public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
@ -106,6 +108,43 @@ public abstract class RedisPoolCacheStore<T> implements CacheStore<T> {
return true; return true;
} }
/**
* 执行Jedis相关操作.
* @param consumer 执行方法
*/
protected void executeJedisCommand(Consumer<Jedis> consumer) {
try (Jedis jedis = this.jedisPool.getResource()) {
consumer.accept(jedis);
}
}
/**
* 执行Jedis相关操作.
* @param function 执行方法
* @param <R> 返回值类型
* @return 返回提供Function函数式接口所返回的东西
*/
protected <R> R executeJedisCommand(Function<Jedis, R> function) {
try (Jedis jedis = this.jedisPool.getResource()) {
return function.apply(jedis);
}
}
@Override
public T getCache(String key, long index, long length) {
return getCache(key);
}
@Override
public long length(String key) {
return -1;
}
@Override
public boolean supportedList() {
return false;
}
/** /**
* 替换原本的分隔符(.)(:).<br/> * 替换原本的分隔符(.)(:).<br/>
* 即将启用 * 即将启用