From 765deae84d167ac40e962914a99b636fb70a3bd7 Mon Sep 17 00:00:00 2001
From: zendwang <wangzhenxian@idadt.com>
Date: 星期一, 04 七月 2022 13:06:18 +0800
Subject: [PATCH] update 优化魔法值

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java |   84 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java
index aa5c0dc..7ed3b28 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java
@@ -6,11 +6,11 @@
 import lombok.NoArgsConstructor;
 import org.redisson.api.*;
 
+import java.time.Duration;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
 
 /**
@@ -107,7 +107,7 @@
             } catch (Exception e) {
                 long timeToLive = bucket.remainTimeToLive();
                 bucket.set(value);
-                bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
+                bucket.expire(Duration.ofMillis(timeToLive));
             }
         } else {
             bucket.set(value);
@@ -119,18 +119,17 @@
      *
      * @param key      缂撳瓨鐨勯敭鍊�
      * @param value    缂撳瓨鐨勫��
-     * @param timeout  鏃堕棿
-     * @param timeUnit 鏃堕棿棰楃矑搴�
+     * @param duration 鏃堕棿
      */
-    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 Duration duration) {
         RBucket<T> result = CLIENT.getBucket(key);
         result.set(value);
-        result.expire(timeout, timeUnit);
+        result.expire(duration);
     }
 
     /**
      * 娉ㄥ唽瀵硅薄鐩戝惉鍣�
-     *
+     * <p>
      * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆
      *
      * @param key      缂撳瓨鐨勯敭鍊�
@@ -149,20 +148,19 @@
      * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触
      */
     public static boolean expire(final String key, final long timeout) {
-        return expire(key, timeout, TimeUnit.SECONDS);
+        return expire(key, Duration.ofSeconds(timeout));
     }
 
     /**
      * 璁剧疆鏈夋晥鏃堕棿
      *
-     * @param key     Redis閿�
-     * @param timeout 瓒呮椂鏃堕棿
-     * @param unit    鏃堕棿鍗曚綅
+     * @param key      Redis閿�
+     * @param duration 瓒呮椂鏃堕棿
      * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触
      */
-    public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
+    public static boolean expire(final String key, final Duration duration) {
         RBucket rBucket = CLIENT.getBucket(key);
-        return rBucket.expire(timeout, unit);
+        return rBucket.expire(duration);
     }
 
     /**
@@ -223,7 +221,7 @@
 
     /**
      * 娉ㄥ唽List鐩戝惉鍣�
-     *
+     * <p>
      * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆
      *
      * @param key      缂撳瓨鐨勯敭鍊�
@@ -259,7 +257,7 @@
 
     /**
      * 娉ㄥ唽Set鐩戝惉鍣�
-     *
+     * <p>
      * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆
      *
      * @param key      缂撳瓨鐨勯敭鍊�
@@ -296,7 +294,7 @@
 
     /**
      * 娉ㄥ唽Map鐩戝惉鍣�
-     *
+     * <p>
      * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆
      *
      * @param key      缂撳瓨鐨勯敭鍊�
@@ -367,6 +365,50 @@
     }
 
     /**
+     * 璁剧疆鍘熷瓙鍊�
+     *
+     * @param key   Redis閿�
+     * @param value 鍊�
+     */
+    public static void setAtomicValue(String key, long value) {
+        RAtomicLong atomic = CLIENT.getAtomicLong(key);
+        atomic.set(value);
+    }
+
+    /**
+     * 鑾峰彇鍘熷瓙鍊�
+     *
+     * @param key Redis閿�
+     * @return 褰撳墠鍊�
+     */
+    public static long getAtomicValue(String key) {
+        RAtomicLong atomic = CLIENT.getAtomicLong(key);
+        return atomic.get();
+    }
+
+    /**
+     * 閫掑鍘熷瓙鍊�
+     *
+     * @param key Redis閿�
+     * @return 褰撳墠鍊�
+     */
+    public static long incrAtomicValue(String key) {
+        RAtomicLong atomic = CLIENT.getAtomicLong(key);
+        return atomic.incrementAndGet();
+    }
+
+    /**
+     * 閫掑噺鍘熷瓙鍊�
+     *
+     * @param key Redis閿�
+     * @return 褰撳墠鍊�
+     */
+    public static long decrAtomicValue(String key) {
+        RAtomicLong atomic = CLIENT.getAtomicLong(key);
+        return atomic.decrementAndGet();
+    }
+
+    /**
      * 鑾峰緱缂撳瓨鐨勫熀鏈璞″垪琛�
      *
      * @param pattern 瀛楃涓插墠缂�
@@ -376,4 +418,14 @@
         Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern);
         return IterUtil.toList(iterable);
     }
+
+    /**
+     * 妫�鏌edis涓槸鍚﹀瓨鍦╧ey
+     *
+     * @param key 閿�
+     */
+    public static Boolean hasKey(String key) {
+        RKeys rKeys = CLIENT.getKeys();
+        return rKeys.countExists(key) > 0;
+    }
 }

--
Gitblit v1.9.3