[Change] CacheStore-Redis 迁移工具方法到 RedisUtils;

[Change] RedisCacheStore, RedisUtils 迁移 'getKeyString(CacheKey)' 方法的具体实现到 RedisUtils (方法: 'toRedisCacheKey(String, CacheKey)');
[Add] RedisUtils 添加 CACHE_KEY_ALL 以构建对 CacheStore 所有 Key 的匹配规则;
This commit is contained in:
LamGC 2021-01-11 16:44:22 +08:00
parent a6fc04c07e
commit 8108379f96
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 20 additions and 2 deletions

View File

@ -44,7 +44,7 @@ public abstract class RedisCacheStore<V> implements CacheStore<V> {
* @return 返回 Key 前缀.
*/
protected String getKeyString(CacheKey cacheKey) {
return getKeyPrefix() + cacheKey.join(RedisUtils.KEY_SEPARATOR);
return RedisUtils.toRedisCacheKey(getKeyPrefix(), cacheKey);
}
/**

View File

@ -17,6 +17,8 @@
package net.lamgc.cgj.bot.cache.redis;
import net.lamgc.cgj.bot.cache.CacheKey;
/**
* @author LamGC
*/
@ -33,10 +35,15 @@ public class RedisUtils {
public final static int RETURN_CODE_FAILED = 0;
/**
* Key匹配规则 - 所有Key
* Key 匹配规则 - 所有 Key
*/
public final static String KEY_PATTERN_ALL = "*";
/**
* 特殊缓存键 - 所有 Key.
*/
public final static CacheKey CACHE_KEY_ALL = new CacheKey("*");
/**
* Key 分隔符
*/
@ -51,4 +58,15 @@ public class RedisUtils {
return "OK".equalsIgnoreCase(result);
}
/**
* {@link CacheKey} 转换为 Redis 的标准 key 格式.
* @param keyPrefix Key 前缀.
* @param cacheKey 缓存键对象.
* @return 返回格式化后的 Key.
*/
public static String toRedisCacheKey(String keyPrefix, CacheKey cacheKey) {
return (keyPrefix.endsWith(KEY_SEPARATOR) ? keyPrefix : (keyPrefix + KEY_SEPARATOR)) +
cacheKey.join(RedisUtils.KEY_SEPARATOR);
}
}