mirror of https://gitee.com/stylefeng/roses
【7.1.1】【cache】缓存key优化,统一处理为大写
parent
4345b07493
commit
cc7055fbbe
|
@ -24,6 +24,8 @@
|
||||||
*/
|
*/
|
||||||
package cn.stylefeng.roses.kernel.cache.api;
|
package cn.stylefeng.roses.kernel.cache.api;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -134,4 +136,22 @@ public interface CacheOperatorApi<T> {
|
||||||
*/
|
*/
|
||||||
String getCommonKeyPrefix();
|
String getCommonKeyPrefix();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算最终插入缓存的key值
|
||||||
|
* <p>
|
||||||
|
* key的组成: 缓存前缀 + keyParam.toUpperCase
|
||||||
|
*
|
||||||
|
* @param keyParam 用户传递的key参数
|
||||||
|
* @return 最终插入缓存的key值
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/7/30 21:18
|
||||||
|
*/
|
||||||
|
default String calcKey(String keyParam) {
|
||||||
|
if (StrUtil.isBlank(keyParam)) {
|
||||||
|
return getCommonKeyPrefix();
|
||||||
|
} else {
|
||||||
|
return getCommonKeyPrefix() + keyParam.toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,38 +48,38 @@ public abstract class AbstractMemoryCacheOperator<T> implements CacheOperatorApi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(String key, T value) {
|
public void put(String key, T value) {
|
||||||
timedCache.put(getCommonKeyPrefix() + key, value);
|
timedCache.put(calcKey(key), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(String key, T value, Long timeoutSeconds) {
|
public void put(String key, T value, Long timeoutSeconds) {
|
||||||
timedCache.put(getCommonKeyPrefix() + key, value, timeoutSeconds * 1000);
|
timedCache.put(calcKey(key), value, timeoutSeconds * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(String key) {
|
public T get(String key) {
|
||||||
// 如果用户在超时前调用了get(key)方法,会重头计算起始时间,false的作用就是不从头算
|
// 如果用户在超时前调用了get(key)方法,会重头计算起始时间,false的作用就是不从头算
|
||||||
return timedCache.get(getCommonKeyPrefix() + key, true);
|
return timedCache.get(calcKey(key), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(String... key) {
|
public void remove(String... key) {
|
||||||
if (key.length > 0) {
|
if (key.length > 0) {
|
||||||
for (String itemKey : key) {
|
for (String itemKey : key) {
|
||||||
timedCache.remove(getCommonKeyPrefix() + itemKey);
|
timedCache.remove(calcKey(itemKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expire(String key, Long expiredSeconds) {
|
public void expire(String key, Long expiredSeconds) {
|
||||||
T value = timedCache.get(getCommonKeyPrefix() + key, true);
|
T value = timedCache.get(calcKey(key), true);
|
||||||
timedCache.put(getCommonKeyPrefix() + key, value, expiredSeconds * 1000);
|
timedCache.put(calcKey(key), value, expiredSeconds * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(String key) {
|
public boolean contains(String key) {
|
||||||
return timedCache.containsKey(getCommonKeyPrefix() + key);
|
return timedCache.containsKey(calcKey(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -50,34 +50,34 @@ public abstract class AbstractRedisCacheOperator<T> implements CacheOperatorApi<
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(String key, T value) {
|
public void put(String key, T value) {
|
||||||
redisTemplate.boundValueOps(getCommonKeyPrefix() + key).set(value);
|
redisTemplate.boundValueOps(calcKey(key)).set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(String key, T value, Long timeoutSeconds) {
|
public void put(String key, T value, Long timeoutSeconds) {
|
||||||
redisTemplate.boundValueOps(getCommonKeyPrefix() + key).set(value, timeoutSeconds, TimeUnit.SECONDS);
|
redisTemplate.boundValueOps(calcKey(key)).set(value, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T get(String key) {
|
public T get(String key) {
|
||||||
return redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get();
|
return redisTemplate.boundValueOps(calcKey(key)).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(String... key) {
|
public void remove(String... key) {
|
||||||
ArrayList<String> keys = CollectionUtil.toList(key);
|
ArrayList<String> keys = CollectionUtil.toList(key);
|
||||||
List<String> withPrefixKeys = keys.stream().map(i -> getCommonKeyPrefix() + i).collect(Collectors.toList());
|
List<String> withPrefixKeys = keys.stream().map(this::calcKey).collect(Collectors.toList());
|
||||||
redisTemplate.delete(withPrefixKeys);
|
redisTemplate.delete(withPrefixKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expire(String key, Long expiredSeconds) {
|
public void expire(String key, Long expiredSeconds) {
|
||||||
redisTemplate.boundValueOps(getCommonKeyPrefix() + key).expire(expiredSeconds, TimeUnit.SECONDS);
|
redisTemplate.boundValueOps(calcKey(key)).expire(expiredSeconds, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(String key) {
|
public boolean contains(String key) {
|
||||||
T value = redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get();
|
T value = redisTemplate.boundValueOps(calcKey(key)).get();
|
||||||
return value != null;
|
return value != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue