【7.3.2】【cache】重新整理缓存key组成

pull/41/head
fengshuonan 2022-11-09 11:10:31 +08:00
parent 5195b1c20c
commit 88903ab02d
3 changed files with 52 additions and 11 deletions

View File

@ -142,10 +142,26 @@ public interface CacheOperatorApi<T> {
*/ */
String getCommonKeyPrefix(); String getCommonKeyPrefix();
/**
*
* <p>
* key::key
*
* @author fengshuonan
* @date 2022/11/9 10:41
*/
default String getFinalPrefix() {
// 获取租户前缀
String tenantPrefix = getTenantPrefix();
// 计算最终前缀
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix() + CACHE_DELIMITER;
}
/** /**
* key * key
* <p> * <p>
* key + keyParam.toUpperCase * key::key
* *
* @param keyParam key * @param keyParam key
* @return key * @return key
@ -153,6 +169,37 @@ public interface CacheOperatorApi<T> {
* @date 2021/7/30 21:18 * @date 2021/7/30 21:18
*/ */
default String calcKey(String keyParam) { default String calcKey(String keyParam) {
if (StrUtil.isEmpty(keyParam)) {
return getFinalPrefix();
} else {
return getFinalPrefix() + keyParam;
}
}
/**
* keykey
*
* @param finalKey CacheOperatorkey
* @return key
* @author fengshuonan
* @date 2022/11/9 10:31
*/
default String removePrefix(String finalKey) {
if (ObjectUtil.isEmpty(finalKey)) {
return "";
}
return StrUtil.removePrefix(finalKey, getFinalPrefix());
}
/**
*
*
* @author fengshuonan
* @date 2022/11/9 10:35
*/
default String getTenantPrefix() {
// 用户的租户前缀 // 用户的租户前缀
String tenantPrefix = ""; String tenantPrefix = "";
@ -170,11 +217,7 @@ public interface CacheOperatorApi<T> {
tenantPrefix = TenantConstants.MASTER_DATASOURCE_NAME; tenantPrefix = TenantConstants.MASTER_DATASOURCE_NAME;
} }
if (StrUtil.isBlank(keyParam)) { return tenantPrefix;
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix();
} else {
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix() + CACHE_DELIMITER + keyParam.toUpperCase();
}
} }
} }

View File

@ -28,7 +28,6 @@ import cn.hutool.cache.impl.CacheObj;
import cn.hutool.cache.impl.TimedCache; import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import java.util.*; import java.util.*;
@ -90,7 +89,7 @@ public abstract class AbstractMemoryCacheOperator<T> implements CacheOperatorApi
while (cacheObjIterator.hasNext()) { while (cacheObjIterator.hasNext()) {
// 去掉缓存key的common prefix前缀 // 去掉缓存key的common prefix前缀
String key = cacheObjIterator.next().getKey(); String key = cacheObjIterator.next().getKey();
keys.add(StrUtil.removePrefix(key, getCommonKeyPrefix())); keys.add(removePrefix(key));
} }
return keys; return keys;
} }

View File

@ -26,7 +26,6 @@ package cn.stylefeng.roses.kernel.cache.redis;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -83,10 +82,10 @@ public abstract class AbstractRedisCacheOperator<T> implements CacheOperatorApi<
@Override @Override
public Collection<String> getAllKeys() { public Collection<String> getAllKeys() {
Set<String> keys = redisTemplate.keys(getCommonKeyPrefix() + "*"); Set<String> keys = redisTemplate.keys(getFinalPrefix() + "*");
if (keys != null) { if (keys != null) {
// 去掉缓存key的common prefix前缀 // 去掉缓存key的common prefix前缀
return keys.stream().map(key -> StrUtil.removePrefix(key, getCommonKeyPrefix())).collect(Collectors.toSet()); return keys.stream().map(this::removePrefix).collect(Collectors.toSet());
} else { } else {
return CollectionUtil.newHashSet(); return CollectionUtil.newHashSet();
} }