|
|
@@ -1,5 +1,6 @@
|
|
|
-package com.cec.plugins.utils;
|
|
|
+package com.cec.plugins.core;
|
|
|
|
|
|
+import com.cec.plugins.utils.SpringUtils;
|
|
|
import org.redisson.api.*;
|
|
|
import org.redisson.config.Config;
|
|
|
|
|
|
@@ -15,12 +16,13 @@ import java.util.stream.Stream;
|
|
|
* @date 2023/8/11 18:15
|
|
|
* @description Redis工具类
|
|
|
*/
|
|
|
-public class RedisUtils {
|
|
|
+public abstract class RedisManage {
|
|
|
|
|
|
- private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);
|
|
|
+ /** 获取客户端实例 */
|
|
|
+ public abstract RedissonClient getClient();
|
|
|
|
|
|
- public static NameMapper getNameMapper() {
|
|
|
- Config config = CLIENT.getConfig();
|
|
|
+ public NameMapper getNameMapper() {
|
|
|
+ Config config = getClient().getConfig();
|
|
|
if (config.isClusterConfig()) {
|
|
|
return config.useClusterServers().getNameMapper();
|
|
|
}
|
|
|
@@ -36,8 +38,8 @@ public class RedisUtils {
|
|
|
* @param rateInterval 速率间隔
|
|
|
* @return -1 表示失败
|
|
|
*/
|
|
|
- public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
|
|
|
- RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);
|
|
|
+ public long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
|
|
|
+ RRateLimiter rateLimiter = getClient().getRateLimiter(key);
|
|
|
rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
|
|
|
if (rateLimiter.tryAcquire()) {
|
|
|
return rateLimiter.availablePermits();
|
|
|
@@ -46,13 +48,6 @@ public class RedisUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取客户端实例
|
|
|
- */
|
|
|
- public static RedissonClient getClient() {
|
|
|
- return CLIENT;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 发布通道消息
|
|
|
*
|
|
|
@@ -60,14 +55,14 @@ public class RedisUtils {
|
|
|
* @param msg 发送数据
|
|
|
* @param consumer 自定义处理
|
|
|
*/
|
|
|
- public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
|
|
|
- RTopic topic = CLIENT.getTopic(channelKey);
|
|
|
+ public <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
|
|
|
+ RTopic topic = getClient().getTopic(channelKey);
|
|
|
topic.publish(msg);
|
|
|
consumer.accept(msg);
|
|
|
}
|
|
|
|
|
|
- public static <T> void publish(String channelKey, T msg) {
|
|
|
- RTopic topic = CLIENT.getTopic(channelKey);
|
|
|
+ public <T> void publish(String channelKey, T msg) {
|
|
|
+ RTopic topic = getClient().getTopic(channelKey);
|
|
|
topic.publish(msg);
|
|
|
}
|
|
|
|
|
|
@@ -78,8 +73,8 @@ public class RedisUtils {
|
|
|
* @param clazz 消息类型
|
|
|
* @param consumer 自定义处理
|
|
|
*/
|
|
|
- public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
|
|
|
- RTopic topic = CLIENT.getTopic(channelKey);
|
|
|
+ public <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
|
|
|
+ RTopic topic = getClient().getTopic(channelKey);
|
|
|
topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
|
|
|
}
|
|
|
|
|
|
@@ -89,7 +84,7 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param value 缓存的值
|
|
|
*/
|
|
|
- public static <T> void setCacheObject(final String key, final T value) {
|
|
|
+ public <T> void setCacheObject(final String key, final T value) {
|
|
|
setCacheObject(key, value, false);
|
|
|
}
|
|
|
|
|
|
@@ -101,8 +96,8 @@ public class RedisUtils {
|
|
|
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
|
|
|
* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
|
|
|
*/
|
|
|
- public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
|
|
|
- RBucket<T> bucket = CLIENT.getBucket(key);
|
|
|
+ public <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
|
|
|
+ RBucket<T> bucket = getClient().getBucket(key);
|
|
|
if (isSaveTtl) {
|
|
|
try {
|
|
|
bucket.setAndKeepTTL(value);
|
|
|
@@ -122,8 +117,8 @@ public class RedisUtils {
|
|
|
* @param value 缓存的值
|
|
|
* @param duration 时间
|
|
|
*/
|
|
|
- public static <T> void setCacheObject(final String key, final T value, final Duration duration) {
|
|
|
- RBatch batch = CLIENT.createBatch();
|
|
|
+ public <T> void setCacheObject(final String key, final T value, final Duration duration) {
|
|
|
+ RBatch batch = getClient().createBatch();
|
|
|
RBucketAsync<T> bucket = batch.getBucket(key);
|
|
|
bucket.setAsync(value);
|
|
|
bucket.expireAsync(duration);
|
|
|
@@ -138,8 +133,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param listener 监听器配置
|
|
|
*/
|
|
|
- public static <T> void addObjectListener(final String key, final ObjectListener listener) {
|
|
|
- RBucket<T> result = CLIENT.getBucket(key);
|
|
|
+ public <T> void addObjectListener(final String key, final ObjectListener listener) {
|
|
|
+ RBucket<T> result = getClient().getBucket(key);
|
|
|
result.addListener(listener);
|
|
|
}
|
|
|
|
|
|
@@ -150,7 +145,7 @@ public class RedisUtils {
|
|
|
* @param timeout 超时时间
|
|
|
* @return true=设置成功;false=设置失败
|
|
|
*/
|
|
|
- public static boolean expire(final String key, final long timeout) {
|
|
|
+ public boolean expire(final String key, final long timeout) {
|
|
|
return expire(key, Duration.ofSeconds(timeout));
|
|
|
}
|
|
|
|
|
|
@@ -161,8 +156,8 @@ public class RedisUtils {
|
|
|
* @param duration 超时时间
|
|
|
* @return true=设置成功;false=设置失败
|
|
|
*/
|
|
|
- public static boolean expire(final String key, final Duration duration) {
|
|
|
- RBucket<Object> rBucket = CLIENT.getBucket(key);
|
|
|
+ public boolean expire(final String key, final Duration duration) {
|
|
|
+ RBucket<Object> rBucket = getClient().getBucket(key);
|
|
|
return rBucket.expire(duration);
|
|
|
}
|
|
|
|
|
|
@@ -172,8 +167,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存键值
|
|
|
* @return 缓存键值对应的数据
|
|
|
*/
|
|
|
- public static <T> T getCacheObject(final String key) {
|
|
|
- RBucket<T> rBucket = CLIENT.getBucket(key);
|
|
|
+ public <T> T getCacheObject(final String key) {
|
|
|
+ RBucket<T> rBucket = getClient().getBucket(key);
|
|
|
return rBucket.get();
|
|
|
}
|
|
|
|
|
|
@@ -183,8 +178,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存键值
|
|
|
* @return 剩余存活时间
|
|
|
*/
|
|
|
- public static <T> long getTimeToLive(final String key) {
|
|
|
- RBucket<T> rBucket = CLIENT.getBucket(key);
|
|
|
+ public <T> long getTimeToLive(final String key) {
|
|
|
+ RBucket<T> rBucket = getClient().getBucket(key);
|
|
|
return rBucket.remainTimeToLive();
|
|
|
}
|
|
|
|
|
|
@@ -193,8 +188,8 @@ public class RedisUtils {
|
|
|
*
|
|
|
* @param key 缓存的键值
|
|
|
*/
|
|
|
- public static boolean deleteObject(final String key) {
|
|
|
- return CLIENT.getBucket(key).delete();
|
|
|
+ public boolean deleteObject(final String key) {
|
|
|
+ return getClient().getBucket(key).delete();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -202,8 +197,8 @@ public class RedisUtils {
|
|
|
*
|
|
|
* @param collection 多个对象
|
|
|
*/
|
|
|
- public static void deleteObject(final Collection collection) {
|
|
|
- RBatch batch = CLIENT.createBatch();
|
|
|
+ public void deleteObject(final Collection collection) {
|
|
|
+ RBatch batch = getClient().createBatch();
|
|
|
collection.forEach(t -> {
|
|
|
batch.getBucket(t.toString()).deleteAsync();
|
|
|
});
|
|
|
@@ -217,8 +212,8 @@ public class RedisUtils {
|
|
|
* @param dataList 待缓存的List数据
|
|
|
* @return 缓存的对象
|
|
|
*/
|
|
|
- public static <T> boolean setCacheList(final String key, final List<T> dataList) {
|
|
|
- RList<T> rList = CLIENT.getList(key);
|
|
|
+ public <T> boolean setCacheList(final String key, final List<T> dataList) {
|
|
|
+ RList<T> rList = getClient().getList(key);
|
|
|
return rList.addAll(dataList);
|
|
|
}
|
|
|
|
|
|
@@ -230,8 +225,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param listener 监听器配置
|
|
|
*/
|
|
|
- public static <T> void addListListener(final String key, final ObjectListener listener) {
|
|
|
- RList<T> rList = CLIENT.getList(key);
|
|
|
+ public <T> void addListListener(final String key, final ObjectListener listener) {
|
|
|
+ RList<T> rList = getClient().getList(key);
|
|
|
rList.addListener(listener);
|
|
|
}
|
|
|
|
|
|
@@ -241,8 +236,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @return 缓存键值对应的数据
|
|
|
*/
|
|
|
- public static <T> List<T> getCacheList(final String key) {
|
|
|
- RList<T> rList = CLIENT.getList(key);
|
|
|
+ public <T> List<T> getCacheList(final String key) {
|
|
|
+ RList<T> rList = getClient().getList(key);
|
|
|
return rList.readAll();
|
|
|
}
|
|
|
|
|
|
@@ -253,8 +248,8 @@ public class RedisUtils {
|
|
|
* @param dataSet 缓存的数据
|
|
|
* @return 缓存数据的对象
|
|
|
*/
|
|
|
- public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
|
|
|
- RSet<T> rSet = CLIENT.getSet(key);
|
|
|
+ public <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
|
|
|
+ RSet<T> rSet = getClient().getSet(key);
|
|
|
return rSet.addAll(dataSet);
|
|
|
}
|
|
|
|
|
|
@@ -266,8 +261,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param listener 监听器配置
|
|
|
*/
|
|
|
- public static <T> void addSetListener(final String key, final ObjectListener listener) {
|
|
|
- RSet<T> rSet = CLIENT.getSet(key);
|
|
|
+ public <T> void addSetListener(final String key, final ObjectListener listener) {
|
|
|
+ RSet<T> rSet = getClient().getSet(key);
|
|
|
rSet.addListener(listener);
|
|
|
}
|
|
|
|
|
|
@@ -277,8 +272,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的key
|
|
|
* @return set对象
|
|
|
*/
|
|
|
- public static <T> Set<T> getCacheSet(final String key) {
|
|
|
- RSet<T> rSet = CLIENT.getSet(key);
|
|
|
+ public <T> Set<T> getCacheSet(final String key) {
|
|
|
+ RSet<T> rSet = getClient().getSet(key);
|
|
|
return rSet.readAll();
|
|
|
}
|
|
|
|
|
|
@@ -288,9 +283,9 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param dataMap 缓存的数据
|
|
|
*/
|
|
|
- public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
|
|
+ public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
|
|
if (dataMap != null) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
rMap.putAll(dataMap);
|
|
|
}
|
|
|
}
|
|
|
@@ -303,8 +298,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @param listener 监听器配置
|
|
|
*/
|
|
|
- public static <T> void addMapListener(final String key, final ObjectListener listener) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> void addMapListener(final String key, final ObjectListener listener) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
rMap.addListener(listener);
|
|
|
}
|
|
|
|
|
|
@@ -314,8 +309,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @return map对象
|
|
|
*/
|
|
|
- public static <T> Map<String, T> getCacheMap(final String key) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> Map<String, T> getCacheMap(final String key) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
return rMap.getAll(rMap.keySet());
|
|
|
}
|
|
|
|
|
|
@@ -325,8 +320,8 @@ public class RedisUtils {
|
|
|
* @param key 缓存的键值
|
|
|
* @return key列表
|
|
|
*/
|
|
|
- public static <T> Set<String> getCacheMapKeySet(final String key) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> Set<String> getCacheMapKeySet(final String key) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
return rMap.keySet();
|
|
|
}
|
|
|
|
|
|
@@ -337,8 +332,8 @@ public class RedisUtils {
|
|
|
* @param hKey Hash键
|
|
|
* @param value 值
|
|
|
*/
|
|
|
- public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
rMap.put(hKey, value);
|
|
|
}
|
|
|
|
|
|
@@ -349,8 +344,8 @@ public class RedisUtils {
|
|
|
* @param hKey Hash键
|
|
|
* @return Hash中的对象
|
|
|
*/
|
|
|
- public static <T> T getCacheMapValue(final String key, final String hKey) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> T getCacheMapValue(final String key, final String hKey) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
return rMap.get(hKey);
|
|
|
}
|
|
|
|
|
|
@@ -361,8 +356,8 @@ public class RedisUtils {
|
|
|
* @param hKey Hash键
|
|
|
* @return Hash中的对象
|
|
|
*/
|
|
|
- public static <T> T delCacheMapValue(final String key, final String hKey) {
|
|
|
- RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
+ public <T> T delCacheMapValue(final String key, final String hKey) {
|
|
|
+ RMap<String, T> rMap = getClient().getMap(key);
|
|
|
return rMap.remove(hKey);
|
|
|
}
|
|
|
|
|
|
@@ -373,8 +368,8 @@ public class RedisUtils {
|
|
|
* @param hKeys Hash键集合
|
|
|
* @return Hash对象集合
|
|
|
*/
|
|
|
- public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
|
|
|
- RMap<K, V> rMap = CLIENT.getMap(key);
|
|
|
+ public <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
|
|
|
+ RMap<K, V> rMap = getClient().getMap(key);
|
|
|
return rMap.getAll(hKeys);
|
|
|
}
|
|
|
|
|
|
@@ -384,8 +379,8 @@ public class RedisUtils {
|
|
|
* @param key Redis键
|
|
|
* @param value 值
|
|
|
*/
|
|
|
- public static void setAtomicValue(String key, long value) {
|
|
|
- RAtomicLong atomic = CLIENT.getAtomicLong(key);
|
|
|
+ public void setAtomicValue(String key, long value) {
|
|
|
+ RAtomicLong atomic = getClient().getAtomicLong(key);
|
|
|
atomic.set(value);
|
|
|
}
|
|
|
|
|
|
@@ -395,8 +390,8 @@ public class RedisUtils {
|
|
|
* @param key Redis键
|
|
|
* @return 当前值
|
|
|
*/
|
|
|
- public static long getAtomicValue(String key) {
|
|
|
- RAtomicLong atomic = CLIENT.getAtomicLong(key);
|
|
|
+ public long getAtomicValue(String key) {
|
|
|
+ RAtomicLong atomic = getClient().getAtomicLong(key);
|
|
|
return atomic.get();
|
|
|
}
|
|
|
|
|
|
@@ -406,8 +401,8 @@ public class RedisUtils {
|
|
|
* @param key Redis键
|
|
|
* @return 当前值
|
|
|
*/
|
|
|
- public static long incrAtomicValue(String key) {
|
|
|
- RAtomicLong atomic = CLIENT.getAtomicLong(key);
|
|
|
+ public long incrAtomicValue(String key) {
|
|
|
+ RAtomicLong atomic = getClient().getAtomicLong(key);
|
|
|
return atomic.incrementAndGet();
|
|
|
}
|
|
|
|
|
|
@@ -417,8 +412,8 @@ public class RedisUtils {
|
|
|
* @param key Redis键
|
|
|
* @return 当前值
|
|
|
*/
|
|
|
- public static long decrAtomicValue(String key) {
|
|
|
- RAtomicLong atomic = CLIENT.getAtomicLong(key);
|
|
|
+ public long decrAtomicValue(String key) {
|
|
|
+ RAtomicLong atomic = getClient().getAtomicLong(key);
|
|
|
return atomic.decrementAndGet();
|
|
|
}
|
|
|
|
|
|
@@ -428,8 +423,8 @@ public class RedisUtils {
|
|
|
* @param pattern 字符串前缀
|
|
|
* @return 对象列表
|
|
|
*/
|
|
|
- public static Collection<String> keys(final String pattern) {
|
|
|
- Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(pattern));
|
|
|
+ public Collection<String> keys(final String pattern) {
|
|
|
+ Stream<String> stream = getClient().getKeys().getKeysStreamByPattern(getNameMapper().map(pattern));
|
|
|
return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
@@ -438,8 +433,8 @@ public class RedisUtils {
|
|
|
*
|
|
|
* @param pattern 字符串前缀
|
|
|
*/
|
|
|
- public static void deleteKeys(final String pattern) {
|
|
|
- CLIENT.getKeys().deleteByPattern(getNameMapper().map(pattern));
|
|
|
+ public void deleteKeys(final String pattern) {
|
|
|
+ getClient().getKeys().deleteByPattern(getNameMapper().map(pattern));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -447,8 +442,8 @@ public class RedisUtils {
|
|
|
*
|
|
|
* @param key 键
|
|
|
*/
|
|
|
- public static Boolean hasKey(String key) {
|
|
|
- RKeys rKeys = CLIENT.getKeys();
|
|
|
+ public Boolean hasKey(String key) {
|
|
|
+ RKeys rKeys = getClient().getKeys();
|
|
|
return rKeys.countExists(getNameMapper().map(key)) > 0;
|
|
|
}
|
|
|
|
|
|
@@ -459,8 +454,8 @@ public class RedisUtils {
|
|
|
* @param value lock value
|
|
|
* @return true 加锁成功, false 加锁失败
|
|
|
*/
|
|
|
- public static boolean tryLock(String key, String value, long timeToLive, TimeUnit timeUnit) {
|
|
|
- RBucket<Object> bucket = CLIENT.getBucket(key);
|
|
|
+ public boolean tryLock(String key, String value, long timeToLive, TimeUnit timeUnit) {
|
|
|
+ RBucket<Object> bucket = getClient().getBucket(key);
|
|
|
return bucket.trySet(value, timeToLive, timeUnit);
|
|
|
}
|
|
|
|
|
|
@@ -470,15 +465,14 @@ public class RedisUtils {
|
|
|
* @param key lock key
|
|
|
* @param value lock value
|
|
|
*/
|
|
|
- public static void unLock(String key, String value) {
|
|
|
- CLIENT.getScript().eval(RScript.Mode.READ_WRITE, DEL_LOCK_SCRIPT, RScript.ReturnType.INTEGER, Collections.singletonList(key), value);
|
|
|
+ public void unLock(String key, String value) {
|
|
|
+ getClient().getScript().eval(RScript.Mode.READ_WRITE, DEL_LOCK_SCRIPT, RScript.ReturnType.INTEGER, Collections.singletonList(key), value);
|
|
|
}
|
|
|
|
|
|
// 解锁脚本
|
|
|
- private static final String DEL_LOCK_SCRIPT =
|
|
|
- "if redis.call(\"get\",KEYS[1]) == ARGV[1] then" + // 如果 KEYS[1] 对应的 Value 值等于 ARGV[1]
|
|
|
- " return redis.call(\"del\",KEYS[1])" + // 删除 KEYS[1]
|
|
|
- " else" + // 否则
|
|
|
- " return 0" + // 返回 0
|
|
|
- " end;";
|
|
|
+ private final String DEL_LOCK_SCRIPT = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then" + // 如果 KEYS[1] 对应的 Value 值等于 ARGV[1]
|
|
|
+ " return redis.call(\"del\",KEYS[1])" + // 删除 KEYS[1]
|
|
|
+ " else" + // 否则
|
|
|
+ " return 0" + // 返回 0
|
|
|
+ " end;";
|
|
|
}
|