From 1010ccca4dfa63f68f3caa87ddaadcfbe33405a5 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 26 十一月 2021 13:34:18 +0800
Subject: [PATCH] update 更新 RedisUtils set保留ttl 兼容 5.X redis版本

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 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 769ee71..7f4d2ca 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
@@ -88,7 +88,30 @@
      * @param value 缂撳瓨鐨勫��
      */
     public static <T> void setCacheObject(final String key, final T value) {
-        client.getBucket(key).set(value);
+        setCacheObject(key, value, false);
+    }
+
+    /**
+     * 缂撳瓨鍩烘湰鐨勫璞★紝淇濈暀褰撳墠瀵硅薄 TTL 鏈夋晥鏈�
+     *
+     * @param key   缂撳瓨鐨勯敭鍊�
+     * @param value 缂撳瓨鐨勫��
+     * @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<Object> bucket = client.getBucket(key);
+        if (isSaveTtl) {
+            try {
+                bucket.setAndKeepTTL(value);
+            } catch (Exception e) {
+                long timeToLive = bucket.remainTimeToLive();
+                bucket.set(value);
+                bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
+            }
+        } else {
+            bucket.set(value);
+        }
     }
 
     /**
@@ -99,7 +122,7 @@
      * @param timeout  鏃堕棿
      * @param timeUnit 鏃堕棿棰楃矑搴�
      */
-    public static <T> void setCacheObject(final String key, final T value, final Integer 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);
         result.set(value);
         result.expire(timeout, timeUnit);
@@ -138,6 +161,17 @@
     public static <T> T getCacheObject(final String 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();
     }
 
     /**
@@ -260,6 +294,18 @@
     }
 
     /**
+     * 鍒犻櫎Hash涓殑鏁版嵁
+     *
+     * @param key  Redis閿�
+     * @param hKey Hash閿�
+     * @return Hash涓殑瀵硅薄
+     */
+    public static <T> T delCacheMapValue(final String key, final String hKey) {
+        RMap<String, T> rMap = client.getMap(key);
+        return rMap.remove(hKey);
+    }
+
+    /**
      * 鑾峰彇澶氫釜Hash涓殑鏁版嵁
      *
      * @param key   Redis閿�

--
Gitblit v1.9.3