diff --git a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java index 710e82dc6..3fe6a8317 100644 --- a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java +++ b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java @@ -24,6 +24,8 @@ */ package cn.stylefeng.roses.kernel.cache.api; +import cn.hutool.core.util.StrUtil; + import java.util.Collection; import java.util.Map; @@ -134,4 +136,22 @@ public interface CacheOperatorApi { */ String getCommonKeyPrefix(); + /** + * 计算最终插入缓存的key值 + *

+ * 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(); + } + } + } diff --git a/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/memory/AbstractMemoryCacheOperator.java b/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/memory/AbstractMemoryCacheOperator.java index 907fea07d..9d9de6fa2 100644 --- a/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/memory/AbstractMemoryCacheOperator.java +++ b/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/memory/AbstractMemoryCacheOperator.java @@ -48,38 +48,38 @@ public abstract class AbstractMemoryCacheOperator implements CacheOperatorApi @Override public void put(String key, T value) { - timedCache.put(getCommonKeyPrefix() + key, value); + timedCache.put(calcKey(key), value); } @Override public void put(String key, T value, Long timeoutSeconds) { - timedCache.put(getCommonKeyPrefix() + key, value, timeoutSeconds * 1000); + timedCache.put(calcKey(key), value, timeoutSeconds * 1000); } @Override public T get(String key) { // 如果用户在超时前调用了get(key)方法,会重头计算起始时间,false的作用就是不从头算 - return timedCache.get(getCommonKeyPrefix() + key, true); + return timedCache.get(calcKey(key), true); } @Override public void remove(String... key) { if (key.length > 0) { for (String itemKey : key) { - timedCache.remove(getCommonKeyPrefix() + itemKey); + timedCache.remove(calcKey(itemKey)); } } } @Override public void expire(String key, Long expiredSeconds) { - T value = timedCache.get(getCommonKeyPrefix() + key, true); - timedCache.put(getCommonKeyPrefix() + key, value, expiredSeconds * 1000); + T value = timedCache.get(calcKey(key), true); + timedCache.put(calcKey(key), value, expiredSeconds * 1000); } @Override public boolean contains(String key) { - return timedCache.containsKey(getCommonKeyPrefix() + key); + return timedCache.containsKey(calcKey(key)); } @Override diff --git a/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/redis/AbstractRedisCacheOperator.java b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/redis/AbstractRedisCacheOperator.java index 27bd3cced..1125271fc 100644 --- a/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/redis/AbstractRedisCacheOperator.java +++ b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/redis/AbstractRedisCacheOperator.java @@ -50,34 +50,34 @@ public abstract class AbstractRedisCacheOperator implements CacheOperatorApi< @Override public void put(String key, T value) { - redisTemplate.boundValueOps(getCommonKeyPrefix() + key).set(value); + redisTemplate.boundValueOps(calcKey(key)).set(value); } @Override 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 public T get(String key) { - return redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get(); + return redisTemplate.boundValueOps(calcKey(key)).get(); } @Override public void remove(String... key) { ArrayList keys = CollectionUtil.toList(key); - List withPrefixKeys = keys.stream().map(i -> getCommonKeyPrefix() + i).collect(Collectors.toList()); + List withPrefixKeys = keys.stream().map(this::calcKey).collect(Collectors.toList()); redisTemplate.delete(withPrefixKeys); } @Override public void expire(String key, Long expiredSeconds) { - redisTemplate.boundValueOps(getCommonKeyPrefix() + key).expire(expiredSeconds, TimeUnit.SECONDS); + redisTemplate.boundValueOps(calcKey(key)).expire(expiredSeconds, TimeUnit.SECONDS); } @Override public boolean contains(String key) { - T value = redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get(); + T value = redisTemplate.boundValueOps(calcKey(key)).get(); return value != null; }