|
@@ -1,6 +1,6 @@
|
|
package com.ruoyi.common.utils;
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
|
|
+import cn.hutool.core.collection.IterUtil;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import lombok.AccessLevel;
|
|
import lombok.AccessLevel;
|
|
import lombok.NoArgsConstructor;
|
|
import lombok.NoArgsConstructor;
|
|
@@ -23,7 +23,7 @@ import java.util.function.Consumer;
|
|
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
|
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
|
public class RedisUtils {
|
|
public class RedisUtils {
|
|
|
|
|
|
- private static RedissonClient client = SpringUtils.getBean(RedissonClient.class);
|
|
|
|
|
|
+ private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);
|
|
|
|
|
|
/**
|
|
/**
|
|
* 限流
|
|
* 限流
|
|
@@ -35,7 +35,7 @@ public class RedisUtils {
|
|
* @return -1 表示失败
|
|
* @return -1 表示失败
|
|
*/
|
|
*/
|
|
public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
|
|
public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
|
|
- RRateLimiter rateLimiter = client.getRateLimiter(key);
|
|
|
|
|
|
+ RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);
|
|
rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
|
|
rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
|
|
if (rateLimiter.tryAcquire()) {
|
|
if (rateLimiter.tryAcquire()) {
|
|
return rateLimiter.availablePermits();
|
|
return rateLimiter.availablePermits();
|
|
@@ -45,10 +45,10 @@ public class RedisUtils {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取实例id
|
|
|
|
|
|
+ * 获取客户端实例
|
|
*/
|
|
*/
|
|
- public static String getClientId() {
|
|
|
|
- return client.getId();
|
|
|
|
|
|
+ public static RedissonClient getClient() {
|
|
|
|
+ return CLIENT;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -59,13 +59,13 @@ public class RedisUtils {
|
|
* @param consumer 自定义处理
|
|
* @param consumer 自定义处理
|
|
*/
|
|
*/
|
|
public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
|
|
public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
|
|
- RTopic topic = client.getTopic(channelKey);
|
|
|
|
|
|
+ RTopic topic = CLIENT.getTopic(channelKey);
|
|
topic.publish(msg);
|
|
topic.publish(msg);
|
|
consumer.accept(msg);
|
|
consumer.accept(msg);
|
|
}
|
|
}
|
|
|
|
|
|
public static <T> void publish(String channelKey, T msg) {
|
|
public static <T> void publish(String channelKey, T msg) {
|
|
- RTopic topic = client.getTopic(channelKey);
|
|
|
|
|
|
+ RTopic topic = CLIENT.getTopic(channelKey);
|
|
topic.publish(msg);
|
|
topic.publish(msg);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -77,7 +77,7 @@ public class RedisUtils {
|
|
* @param consumer 自定义处理
|
|
* @param consumer 自定义处理
|
|
*/
|
|
*/
|
|
public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
|
|
public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
|
|
- RTopic topic = client.getTopic(channelKey);
|
|
|
|
|
|
+ RTopic topic = CLIENT.getTopic(channelKey);
|
|
topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
|
|
topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
|
|
}
|
|
}
|
|
|
|
|
|
@@ -94,13 +94,13 @@ public class RedisUtils {
|
|
/**
|
|
/**
|
|
* 缓存基本的对象,保留当前对象 TTL 有效期
|
|
* 缓存基本的对象,保留当前对象 TTL 有效期
|
|
*
|
|
*
|
|
- * @param key 缓存的键值
|
|
|
|
- * @param value 缓存的值
|
|
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param value 缓存的值
|
|
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
|
|
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
|
|
* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
|
|
* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
|
|
*/
|
|
*/
|
|
public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
|
|
public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
|
|
- RBucket<Object> bucket = client.getBucket(key);
|
|
|
|
|
|
+ RBucket<Object> bucket = CLIENT.getBucket(key);
|
|
if (isSaveTtl) {
|
|
if (isSaveTtl) {
|
|
try {
|
|
try {
|
|
bucket.setAndKeepTTL(value);
|
|
bucket.setAndKeepTTL(value);
|
|
@@ -123,12 +123,25 @@ public class RedisUtils {
|
|
* @param timeUnit 时间颗粒度
|
|
* @param timeUnit 时间颗粒度
|
|
*/
|
|
*/
|
|
public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
|
|
public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
|
|
- RBucket<T> result = client.getBucket(key);
|
|
|
|
|
|
+ RBucket<T> result = CLIENT.getBucket(key);
|
|
result.set(value);
|
|
result.set(value);
|
|
result.expire(timeout, timeUnit);
|
|
result.expire(timeout, timeUnit);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 注册对象监听器
|
|
|
|
+ *
|
|
|
|
+ * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
|
|
|
|
+ *
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param listener 监听器配置
|
|
|
|
+ */
|
|
|
|
+ public static <T> void addObjectListener(final String key, final ObjectListener listener) {
|
|
|
|
+ RBucket<T> result = CLIENT.getBucket(key);
|
|
|
|
+ result.addListener(listener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 设置有效时间
|
|
* 设置有效时间
|
|
*
|
|
*
|
|
* @param key Redis键
|
|
* @param key Redis键
|
|
@@ -148,7 +161,7 @@ public class RedisUtils {
|
|
* @return true=设置成功;false=设置失败
|
|
* @return true=设置成功;false=设置失败
|
|
*/
|
|
*/
|
|
public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
|
|
public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
|
|
- RBucket rBucket = client.getBucket(key);
|
|
|
|
|
|
+ RBucket rBucket = CLIENT.getBucket(key);
|
|
return rBucket.expire(timeout, unit);
|
|
return rBucket.expire(timeout, unit);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -159,7 +172,7 @@ public class RedisUtils {
|
|
* @return 缓存键值对应的数据
|
|
* @return 缓存键值对应的数据
|
|
*/
|
|
*/
|
|
public static <T> T getCacheObject(final String key) {
|
|
public static <T> T getCacheObject(final String key) {
|
|
- RBucket<T> rBucket = client.getBucket(key);
|
|
|
|
|
|
+ RBucket<T> rBucket = CLIENT.getBucket(key);
|
|
return rBucket.get();
|
|
return rBucket.get();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -170,29 +183,26 @@ public class RedisUtils {
|
|
* @return 剩余存活时间
|
|
* @return 剩余存活时间
|
|
*/
|
|
*/
|
|
public static <T> long getTimeToLive(final String key) {
|
|
public static <T> long getTimeToLive(final String key) {
|
|
- RBucket<T> rBucket = client.getBucket(key);
|
|
|
|
|
|
+ RBucket<T> rBucket = CLIENT.getBucket(key);
|
|
return rBucket.remainTimeToLive();
|
|
return rBucket.remainTimeToLive();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 删除单个对象
|
|
* 删除单个对象
|
|
*
|
|
*
|
|
- * @param key
|
|
|
|
|
|
+ * @param key 缓存的键值
|
|
*/
|
|
*/
|
|
public static boolean deleteObject(final String key) {
|
|
public static boolean deleteObject(final String key) {
|
|
- return client.getBucket(key).delete();
|
|
|
|
|
|
+ return CLIENT.getBucket(key).delete();
|
|
}
|
|
}
|
|
|
|
|
|
- /* */
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 删除集合对象
|
|
* 删除集合对象
|
|
*
|
|
*
|
|
* @param collection 多个对象
|
|
* @param collection 多个对象
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
public static void deleteObject(final Collection collection) {
|
|
public static void deleteObject(final Collection collection) {
|
|
- RBatch batch = client.createBatch();
|
|
|
|
|
|
+ RBatch batch = CLIENT.createBatch();
|
|
collection.forEach(t -> {
|
|
collection.forEach(t -> {
|
|
batch.getBucket(t.toString()).deleteAsync();
|
|
batch.getBucket(t.toString()).deleteAsync();
|
|
});
|
|
});
|
|
@@ -207,18 +217,31 @@ public class RedisUtils {
|
|
* @return 缓存的对象
|
|
* @return 缓存的对象
|
|
*/
|
|
*/
|
|
public static <T> boolean setCacheList(final String key, final List<T> dataList) {
|
|
public static <T> boolean setCacheList(final String key, final List<T> dataList) {
|
|
- RList<T> rList = client.getList(key);
|
|
|
|
|
|
+ RList<T> rList = CLIENT.getList(key);
|
|
return rList.addAll(dataList);
|
|
return rList.addAll(dataList);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 注册List监听器
|
|
|
|
+ *
|
|
|
|
+ * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
|
|
|
|
+ *
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param listener 监听器配置
|
|
|
|
+ */
|
|
|
|
+ public static <T> void addListListener(final String key, final ObjectListener listener) {
|
|
|
|
+ RList<T> rList = CLIENT.getList(key);
|
|
|
|
+ rList.addListener(listener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 获得缓存的list对象
|
|
* 获得缓存的list对象
|
|
*
|
|
*
|
|
* @param key 缓存的键值
|
|
* @param key 缓存的键值
|
|
* @return 缓存键值对应的数据
|
|
* @return 缓存键值对应的数据
|
|
*/
|
|
*/
|
|
public static <T> List<T> getCacheList(final String key) {
|
|
public static <T> List<T> getCacheList(final String key) {
|
|
- RList<T> rList = client.getList(key);
|
|
|
|
|
|
+ RList<T> rList = CLIENT.getList(key);
|
|
return rList.readAll();
|
|
return rList.readAll();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -230,42 +253,68 @@ public class RedisUtils {
|
|
* @return 缓存数据的对象
|
|
* @return 缓存数据的对象
|
|
*/
|
|
*/
|
|
public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
|
|
public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
|
|
- RSet<T> rSet = client.getSet(key);
|
|
|
|
|
|
+ RSet<T> rSet = CLIENT.getSet(key);
|
|
return rSet.addAll(dataSet);
|
|
return rSet.addAll(dataSet);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 注册Set监听器
|
|
|
|
+ *
|
|
|
|
+ * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
|
|
|
|
+ *
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param listener 监听器配置
|
|
|
|
+ */
|
|
|
|
+ public static <T> void addSetListener(final String key, final ObjectListener listener) {
|
|
|
|
+ RSet<T> rSet = CLIENT.getSet(key);
|
|
|
|
+ rSet.addListener(listener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 获得缓存的set
|
|
* 获得缓存的set
|
|
*
|
|
*
|
|
- * @param key
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param key 缓存的key
|
|
|
|
+ * @return set对象
|
|
*/
|
|
*/
|
|
public static <T> Set<T> getCacheSet(final String key) {
|
|
public static <T> Set<T> getCacheSet(final String key) {
|
|
- RSet<T> rSet = client.getSet(key);
|
|
|
|
|
|
+ RSet<T> rSet = CLIENT.getSet(key);
|
|
return rSet.readAll();
|
|
return rSet.readAll();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 缓存Map
|
|
* 缓存Map
|
|
*
|
|
*
|
|
- * @param key
|
|
|
|
- * @param dataMap
|
|
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param dataMap 缓存的数据
|
|
*/
|
|
*/
|
|
public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
|
public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
|
|
if (dataMap != null) {
|
|
if (dataMap != null) {
|
|
- RMap<String, T> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
rMap.putAll(dataMap);
|
|
rMap.putAll(dataMap);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 注册Map监听器
|
|
|
|
+ *
|
|
|
|
+ * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
|
|
|
|
+ *
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @param listener 监听器配置
|
|
|
|
+ */
|
|
|
|
+ public static <T> void addMapListener(final String key, final ObjectListener listener) {
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
|
|
+ rMap.addListener(listener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 获得缓存的Map
|
|
* 获得缓存的Map
|
|
*
|
|
*
|
|
- * @param key
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @param key 缓存的键值
|
|
|
|
+ * @return map对象
|
|
*/
|
|
*/
|
|
public static <T> Map<String, T> getCacheMap(final String key) {
|
|
public static <T> Map<String, T> getCacheMap(final String key) {
|
|
- RMap<String, T> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
return rMap.getAll(rMap.keySet());
|
|
return rMap.getAll(rMap.keySet());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -277,7 +326,7 @@ public class RedisUtils {
|
|
* @param value 值
|
|
* @param value 值
|
|
*/
|
|
*/
|
|
public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
|
public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {
|
|
- RMap<String, T> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
rMap.put(hKey, value);
|
|
rMap.put(hKey, value);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -289,7 +338,7 @@ public class RedisUtils {
|
|
* @return Hash中的对象
|
|
* @return Hash中的对象
|
|
*/
|
|
*/
|
|
public static <T> T getCacheMapValue(final String key, final String hKey) {
|
|
public static <T> T getCacheMapValue(final String key, final String hKey) {
|
|
- RMap<String, T> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
return rMap.get(hKey);
|
|
return rMap.get(hKey);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -301,7 +350,7 @@ public class RedisUtils {
|
|
* @return Hash中的对象
|
|
* @return Hash中的对象
|
|
*/
|
|
*/
|
|
public static <T> T delCacheMapValue(final String key, final String hKey) {
|
|
public static <T> T delCacheMapValue(final String key, final String hKey) {
|
|
- RMap<String, T> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<String, T> rMap = CLIENT.getMap(key);
|
|
return rMap.remove(hKey);
|
|
return rMap.remove(hKey);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -313,7 +362,7 @@ public class RedisUtils {
|
|
* @return Hash对象集合
|
|
* @return Hash对象集合
|
|
*/
|
|
*/
|
|
public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
|
|
public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
|
|
- RMap<K, V> rMap = client.getMap(key);
|
|
|
|
|
|
+ RMap<K, V> rMap = CLIENT.getMap(key);
|
|
return rMap.getAll(hKeys);
|
|
return rMap.getAll(hKeys);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -324,7 +373,7 @@ public class RedisUtils {
|
|
* @return 对象列表
|
|
* @return 对象列表
|
|
*/
|
|
*/
|
|
public static Collection<String> keys(final String pattern) {
|
|
public static Collection<String> keys(final String pattern) {
|
|
- Iterable<String> iterable = client.getKeys().getKeysByPattern(pattern);
|
|
|
|
- return Lists.newArrayList(iterable);
|
|
|
|
|
|
+ Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern);
|
|
|
|
+ return IterUtil.toList(iterable);
|
|
}
|
|
}
|
|
}
|
|
}
|