【7.1.1】【cache】缓存key优化,统一处理为大写

pull/22/head
fengshuonan 2021-07-30 22:04:20 +08:00
parent 4345b07493
commit cc7055fbbe
3 changed files with 33 additions and 13 deletions

View File

@ -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();
}
}
} }

View File

@ -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

View File

@ -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;
} }