refactor(cache/redisson): 完善 Redisson 工具类

This commit is contained in:
2024-02-20 22:20:57 +08:00
parent 9bd4583223
commit 9ed2dac00c

View File

@@ -18,7 +18,6 @@ package top.charles7c.continew.starter.cache.redisson.util;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import org.redisson.api.*; import org.redisson.api.*;
import org.redisson.config.Config;
import top.charles7c.continew.starter.core.constant.StringConstants; import top.charles7c.continew.starter.core.constant.StringConstants;
import java.time.Duration; import java.time.Duration;
@@ -44,7 +43,7 @@ public class RedisUtils {
* @param key 键 * @param key 键
* @param value 值 * @param value 值
*/ */
public static <T> void set(final String key, final T value) { public static <T> void set(String key, T value) {
CLIENT.getBucket(key).set(value); CLIENT.getBucket(key).set(value);
} }
@@ -55,7 +54,7 @@ public class RedisUtils {
* @param value 值 * @param value 值
* @param duration 过期时间 * @param duration 过期时间
*/ */
public static <T> void set(final String key, final T value, final Duration duration) { public static <T> void set(String key, T value, Duration duration) {
RBatch batch = CLIENT.createBatch(); RBatch batch = CLIENT.createBatch();
RBucketAsync<T> bucket = batch.getBucket(key); RBucketAsync<T> bucket = batch.getBucket(key);
bucket.setAsync(value); bucket.setAsync(value);
@@ -69,7 +68,7 @@ public class RedisUtils {
* @param key 键 * @param key 键
* @return 值 * @return 值
*/ */
public static <T> T get(final String key) { public static <T> T get(String key) {
RBucket<T> bucket = CLIENT.getBucket(key); RBucket<T> bucket = CLIENT.getBucket(key);
return bucket.get(); return bucket.get();
} }
@@ -80,10 +79,19 @@ public class RedisUtils {
* @param key 键 * @param key 键
* @return true设置成功false设置失败 * @return true设置成功false设置失败
*/ */
public static boolean delete(final String key) { public static boolean delete(String key) {
return CLIENT.getBucket(key).delete(); return CLIENT.getBucket(key).delete();
} }
/**
* 删除缓存
*
* @param pattern 键模式
*/
public static void deleteByPattern(String pattern) {
CLIENT.getKeys().deleteByPattern(pattern);
}
/** /**
* 设置缓存过期时间 * 设置缓存过期时间
* *
@@ -91,7 +99,7 @@ public class RedisUtils {
* @param timeout 过期时间(单位:秒) * @param timeout 过期时间(单位:秒)
* @return true设置成功false设置失败 * @return true设置成功false设置失败
*/ */
public static boolean expire(final String key, final long timeout) { public static boolean expire(String key, long timeout) {
return expire(key, Duration.ofSeconds(timeout)); return expire(key, Duration.ofSeconds(timeout));
} }
@@ -102,7 +110,7 @@ public class RedisUtils {
* @param duration 过期时间 * @param duration 过期时间
* @return true设置成功false设置失败 * @return true设置成功false设置失败
*/ */
public static boolean expire(final String key, final Duration duration) { public static boolean expire(String key, Duration duration) {
return CLIENT.getBucket(key).expire(duration); return CLIENT.getBucket(key).expire(duration);
} }
@@ -112,7 +120,7 @@ public class RedisUtils {
* @param key 键 * @param key 键
* @return 缓存剩余过期时间(单位:毫秒) * @return 缓存剩余过期时间(单位:毫秒)
*/ */
public static long getTimeToLive(final String key) { public static long getTimeToLive(String key) {
return CLIENT.getBucket(key).remainTimeToLive(); return CLIENT.getBucket(key).remainTimeToLive();
} }
@@ -124,18 +132,18 @@ public class RedisUtils {
*/ */
public static boolean hasKey(String key) { public static boolean hasKey(String key) {
RKeys keys = CLIENT.getKeys(); RKeys keys = CLIENT.getKeys();
return keys.countExists(getNameMapper().map(key)) > 0; return keys.countExists(key) > 0;
} }
/** /**
* 查询缓存列表 * 查询缓存列表
* *
* @param keyPattern 键表达 * @param pattern 键
* @return 缓存列表 * @return 缓存列表
*/ */
public static Collection<String> keys(final String keyPattern) { public static Collection<String> keys(String pattern) {
Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(keyPattern)); Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern);
return stream.map(key -> getNameMapper().unmap(key)).toList(); return stream.toList();
} }
/** /**
@@ -162,21 +170,4 @@ public class RedisUtils {
public static String formatKey(String... subKeys) { public static String formatKey(String... subKeys) {
return String.join(StringConstants.COLON, subKeys); return String.join(StringConstants.COLON, subKeys);
} }
/**
* 根据 Redisson 配置,获取名称映射器
*
* @return 名称映射器
*/
private static NameMapper getNameMapper() {
Config config = CLIENT.getConfig();
if (config.isClusterConfig()) {
return config.useClusterServers().getNameMapper();
}
if (config.isSentinelConfig()) {
return config.useSentinelServers().getNameMapper();
}
return config.useSingleServer().getNameMapper();
}
} }