From 5ca038d888922e93bf45c7bd37f3c6dce849dcff Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 24 十二月 2021 11:36:02 +0800
Subject: [PATCH] update 调整监控依赖 从 common 迁移到 framework

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java |  150 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 108 insertions(+), 42 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java
index 89206c9..0ccbe0f 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java
@@ -1,6 +1,6 @@
 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 lombok.AccessLevel;
 import lombok.NoArgsConstructor;
@@ -23,7 +23,7 @@
 @SuppressWarnings(value = {"unchecked", "rawtypes"})
 public class RedisUtils {
 
-    private static RedissonClient client = SpringUtils.getBean(RedissonClient.class);
+    private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);
 
     /**
      * 闄愭祦
@@ -35,7 +35,7 @@
      * @return -1 琛ㄧず澶辫触
      */
     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);
         if (rateLimiter.tryAcquire()) {
             return rateLimiter.availablePermits();
@@ -45,10 +45,10 @@
     }
 
     /**
-     * 鑾峰彇瀹炰緥id
+     * 鑾峰彇瀹㈡埛绔疄渚�
      */
-    public static String getClientId() {
-        return client.getId();
+    public static RedissonClient getClient() {
+        return CLIENT;
     }
 
     /**
@@ -59,13 +59,13 @@
      * @param 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);
         consumer.accept(msg);
     }
 
     public static <T> void publish(String channelKey, T msg) {
-        RTopic topic = client.getTopic(channelKey);
+        RTopic topic = CLIENT.getTopic(channelKey);
         topic.publish(msg);
     }
 
@@ -77,7 +77,7 @@
      * @param 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));
     }
 
@@ -94,15 +94,21 @@
     /**
      * 缂撳瓨鍩烘湰鐨勫璞★紝淇濈暀褰撳墠瀵硅薄 TTL 鏈夋晥鏈�
      *
-     * @param key   缂撳瓨鐨勯敭鍊�
-     * @param value 缂撳瓨鐨勫��
+     * @param key       缂撳瓨鐨勯敭鍊�
+     * @param value     缂撳瓨鐨勫��
      * @param isSaveTtl 鏄惁淇濈暀TTL鏈夋晥鏈�(渚嬪: set涔嬪墠ttl鍓╀綑90 set涔嬪悗杩樻槸涓�90)
-     * @since Redis 6.0.0 浠ヤ笂鏈夋晥
+     * @since Redis 6.X 浠ヤ笂浣跨敤 setAndKeepTTL 鍏煎 5.X 鏂规
      */
     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) {
-            bucket.setAndKeepTTL(value);
+            try {
+                bucket.setAndKeepTTL(value);
+            } catch (Exception e) {
+                long timeToLive = bucket.remainTimeToLive();
+                bucket.set(value);
+                bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
+            }
         } else {
             bucket.set(value);
         }
@@ -116,10 +122,23 @@
      * @param timeout  鏃堕棿
      * @param timeUnit 鏃堕棿棰楃矑搴�
      */
-    public static <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
-        RBucket<T> result = client.getBucket(key);
+    public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
+        RBucket<T> result = CLIENT.getBucket(key);
         result.set(value);
         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);
     }
 
     /**
@@ -142,7 +161,7 @@
      * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触
      */
     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);
     }
 
@@ -153,29 +172,37 @@
      * @return 缂撳瓨閿�煎搴旂殑鏁版嵁
      */
     public static <T> T getCacheObject(final String key) {
-        RBucket<T> rBucket = client.getBucket(key);
+        RBucket<T> rBucket = CLIENT.getBucket(key);
         return rBucket.get();
+    }
+
+    /**
+     * 鑾峰緱key鍓╀綑瀛樻椿鏃堕棿
+     *
+     * @param key 缂撳瓨閿��
+     * @return 鍓╀綑瀛樻椿鏃堕棿
+     */
+    public static <T> long getTimeToLive(final String key) {
+        RBucket<T> rBucket = CLIENT.getBucket(key);
+        return rBucket.remainTimeToLive();
     }
 
     /**
      * 鍒犻櫎鍗曚釜瀵硅薄
      *
-     * @param key
+     * @param key 缂撳瓨鐨勯敭鍊�
      */
     public static boolean deleteObject(final String key) {
-        return client.getBucket(key).delete();
+        return CLIENT.getBucket(key).delete();
     }
-
-    /* */
 
     /**
      * 鍒犻櫎闆嗗悎瀵硅薄
      *
      * @param collection 澶氫釜瀵硅薄
-     * @return
      */
     public static void deleteObject(final Collection collection) {
-        RBatch batch = client.createBatch();
+        RBatch batch = CLIENT.createBatch();
         collection.forEach(t -> {
             batch.getBucket(t.toString()).deleteAsync();
         });
@@ -190,8 +217,21 @@
      * @return 缂撳瓨鐨勫璞�
      */
     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);
+    }
+
+    /**
+     * 娉ㄥ唽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);
     }
 
     /**
@@ -201,7 +241,7 @@
      * @return 缂撳瓨閿�煎搴旂殑鏁版嵁
      */
     public static <T> List<T> getCacheList(final String key) {
-        RList<T> rList = client.getList(key);
+        RList<T> rList = CLIENT.getList(key);
         return rList.readAll();
     }
 
@@ -213,42 +253,68 @@
      * @return 缂撳瓨鏁版嵁鐨勫璞�
      */
     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);
+    }
+
+    /**
+     * 娉ㄥ唽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);
     }
 
     /**
      * 鑾峰緱缂撳瓨鐨剆et
      *
-     * @param key
-     * @return
+     * @param key 缂撳瓨鐨刱ey
+     * @return set瀵硅薄
      */
     public static <T> Set<T> getCacheSet(final String key) {
-        RSet<T> rSet = client.getSet(key);
+        RSet<T> rSet = CLIENT.getSet(key);
         return rSet.readAll();
     }
 
     /**
      * 缂撳瓨Map
      *
-     * @param key
-     * @param dataMap
+     * @param key     缂撳瓨鐨勯敭鍊�
+     * @param dataMap 缂撳瓨鐨勬暟鎹�
      */
     public static <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 = CLIENT.getMap(key);
             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);
+    }
+
+    /**
      * 鑾峰緱缂撳瓨鐨凪ap
      *
-     * @param key
-     * @return
+     * @param key 缂撳瓨鐨勯敭鍊�
+     * @return map瀵硅薄
      */
     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());
     }
 
@@ -260,7 +326,7 @@
      * @param 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);
     }
 
@@ -272,7 +338,7 @@
      * @return Hash涓殑瀵硅薄
      */
     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);
     }
 
@@ -284,7 +350,7 @@
      * @return Hash涓殑瀵硅薄
      */
     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);
     }
 
@@ -296,7 +362,7 @@
      * @return Hash瀵硅薄闆嗗悎
      */
     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);
     }
 
@@ -307,7 +373,7 @@
      * @return 瀵硅薄鍒楄〃
      */
     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);
     }
 }

--
Gitblit v1.9.3