From bcbc7c1d47546e48481d8ef65fe3385f5704ecb8 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 06 十二月 2021 20:22:45 +0800
Subject: [PATCH] update 更新 RedisUtils 增加 注册监听器方法

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java |   73 ++++++++++++++++++++++++++++++------
 1 files changed, 61 insertions(+), 12 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 7104b4d..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
@@ -94,8 +94,8 @@
     /**
      * 缂撳瓨鍩烘湰鐨勫璞★紝淇濈暀褰撳墠瀵硅薄 TTL 鏈夋晥鏈�
      *
-     * @param key   缂撳瓨鐨勯敭鍊�
-     * @param value 缂撳瓨鐨勫��
+     * @param key       缂撳瓨鐨勯敭鍊�
+     * @param value     缂撳瓨鐨勫��
      * @param isSaveTtl 鏄惁淇濈暀TTL鏈夋晥鏈�(渚嬪: set涔嬪墠ttl鍓╀綑90 set涔嬪悗杩樻槸涓�90)
      * @since Redis 6.X 浠ヤ笂浣跨敤 setAndKeepTTL 鍏煎 5.X 鏂规
      */
@@ -126,6 +126,19 @@
         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);
     }
 
     /**
@@ -177,19 +190,16 @@
     /**
      * 鍒犻櫎鍗曚釜瀵硅薄
      *
-     * @param key
+     * @param key 缂撳瓨鐨勯敭鍊�
      */
     public static boolean deleteObject(final String key) {
         return CLIENT.getBucket(key).delete();
     }
 
-    /* */
-
     /**
      * 鍒犻櫎闆嗗悎瀵硅薄
      *
      * @param collection 澶氫釜瀵硅薄
-     * @return
      */
     public static void deleteObject(final Collection collection) {
         RBatch batch = CLIENT.createBatch();
@@ -209,6 +219,19 @@
     public static <T> boolean setCacheList(final String key, final List<T> dataList) {
         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);
     }
 
     /**
@@ -235,10 +258,23 @@
     }
 
     /**
+     * 娉ㄥ唽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);
@@ -248,8 +284,8 @@
     /**
      * 缂撳瓨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) {
@@ -259,10 +295,23 @@
     }
 
     /**
+     * 娉ㄥ唽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);

--
Gitblit v1.9.3