mirror of https://gitee.com/stylefeng/roses
【7.3.2】【cache】重新整理缓存key组成
parent
5195b1c20c
commit
88903ab02d
|
@ -142,10 +142,26 @@ public interface CacheOperatorApi<T> {
|
|||
*/
|
||||
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值
|
||||
* <p>
|
||||
* key的组成: 缓存前缀 + keyParam.toUpperCase
|
||||
* key的组成方式:租户前缀:业务前缀:业务key
|
||||
*
|
||||
* @param keyParam 用户传递的key参数
|
||||
* @return 最终插入缓存的key值
|
||||
|
@ -153,6 +169,37 @@ public interface CacheOperatorApi<T> {
|
|||
* @date 2021/7/30 21:18
|
||||
*/
|
||||
default String calcKey(String keyParam) {
|
||||
if (StrUtil.isEmpty(keyParam)) {
|
||||
return getFinalPrefix();
|
||||
} else {
|
||||
return getFinalPrefix() + keyParam;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存key的前缀,返回用户最原始的key
|
||||
*
|
||||
* @param finalKey 最终存在CacheOperator的key
|
||||
* @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 = "";
|
||||
|
@ -170,11 +217,7 @@ public interface CacheOperatorApi<T> {
|
|||
tenantPrefix = TenantConstants.MASTER_DATASOURCE_NAME;
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(keyParam)) {
|
||||
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix();
|
||||
} else {
|
||||
return tenantPrefix + CACHE_DELIMITER + getCommonKeyPrefix() + CACHE_DELIMITER + keyParam.toUpperCase();
|
||||
}
|
||||
return tenantPrefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ import cn.hutool.cache.impl.CacheObj;
|
|||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -90,7 +89,7 @@ public abstract class AbstractMemoryCacheOperator<T> implements CacheOperatorApi
|
|||
while (cacheObjIterator.hasNext()) {
|
||||
// 去掉缓存key的common prefix前缀
|
||||
String key = cacheObjIterator.next().getKey();
|
||||
keys.add(StrUtil.removePrefix(key, getCommonKeyPrefix()));
|
||||
keys.add(removePrefix(key));
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ package cn.stylefeng.roses.kernel.cache.redis;
|
|||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
|
@ -83,10 +82,10 @@ public abstract class AbstractRedisCacheOperator<T> implements CacheOperatorApi<
|
|||
|
||||
@Override
|
||||
public Collection<String> getAllKeys() {
|
||||
Set<String> keys = redisTemplate.keys(getCommonKeyPrefix() + "*");
|
||||
Set<String> keys = redisTemplate.keys(getFinalPrefix() + "*");
|
||||
if (keys != null) {
|
||||
// 去掉缓存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 {
|
||||
return CollectionUtil.newHashSet();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue