From 7d6921a3cd3c6bcf04bf5503fa78275d09aa26f9 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期一, 01 十二月 2025 16:25:09 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/utils/RedisUtil.java |   89 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/RedisUtil.java b/src/main/java/com/zy/common/utils/RedisUtil.java
index b2df114..76ad76d 100644
--- a/src/main/java/com/zy/common/utils/RedisUtil.java
+++ b/src/main/java/com/zy/common/utils/RedisUtil.java
@@ -1,9 +1,13 @@
 package com.zy.common.utils;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -13,13 +17,19 @@
  * redisTemplate灏佽
  *
  */
-//@Component
+@Component
 public class RedisUtil {
 
-    @Autowired
-    private RedisTemplate<String, Object> redisTemplate;
+//    @Autowired
+//    private RedisTemplate<String, Object> redisTemplate;
 
-    public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Autowired
+    private StringRedisTemplate stringRedisTemplate;
+
+    public RedisUtil(RedisTemplate redisTemplate) {
         this.redisTemplate = redisTemplate;
     }
 
@@ -85,6 +95,31 @@
 
     //============================ String =============================
 
+    public Set<String> searchKeys(String key) {
+        Set<String> keys = redisTemplate.keys(key + "*");
+        return keys;
+    }
+
+    public Set<String> scanKeys(String key, int limit) {
+        return (Set<String>) redisTemplate.execute((org.springframework.data.redis.core.RedisCallback<Set<String>>) connection -> {
+            org.springframework.data.redis.core.ScanOptions options = org.springframework.data.redis.core.ScanOptions.scanOptions().match(key + "*").count(limit).build();
+            org.springframework.data.redis.core.Cursor<byte[]> cursor = connection.scan(options);
+            java.util.LinkedHashSet<String> result = new java.util.LinkedHashSet<>();
+            while (cursor.hasNext()) {
+                result.add(new String(cursor.next()));
+                if (result.size() >= limit) {
+                    break;
+                }
+            }
+            try { cursor.close(); } catch (Exception e) {}
+            return result;
+        });
+    }
+
+    public java.util.List<Object> multiGet(java.util.Collection<String> keys) {
+        return redisTemplate.opsForValue().multiGet(keys);
+    }
+
     /**
      * 鏅�氱紦瀛樿幏鍙�
      *
@@ -93,6 +128,21 @@
      */
     public Object get(String key) {
         return key == null ? null : redisTemplate.opsForValue().get(key);
+    }
+
+    /**
+     * 鑾峰彇鍏ㄩ儴鏁版嵁
+     * @return
+     */
+    public HashMap<Object, Object> getRedis() {
+        Set<String> keys = redisTemplate.keys("*");
+        HashMap<Object, Object> map = new HashMap<>();
+        for (String key : keys) {
+            Object value = redisTemplate.opsForValue().get(key);
+            map.put(key, value);
+        }
+
+        return map;//杩斿洖鍏ㄩ儴鏁版嵁闆嗗悎
     }
 
     /**
@@ -105,6 +155,36 @@
     public boolean set(String key, Object value) {
         try {
             redisTemplate.opsForValue().set(key, value);
+            redisTemplate.execute((RedisCallback<Void>) connection -> null);
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    /**
+     * 鏅�氱紦瀛樻斁鍏�
+     *
+     * @param key   閿�
+     * @param value 鍊�
+     * @return true鎴愬姛 false澶辫触
+     */
+    public boolean setSync(String key, Object value) {
+        try {
+            redisTemplate.opsForValue().set(key, value);
+            redisTemplate.execute((RedisCallback<Void>) connection -> null);
+            long start = System.currentTimeMillis();
+            while (System.currentTimeMillis() - start < 10000) {//鏈夋晥鏈�10s
+                Object o = redisTemplate.opsForValue().get(key);
+                if (o == null) {
+                    continue;
+                }
+
+                if (o.equals(value)) {
+                    break;
+                }
+            }
             return true;
         } catch (Exception e) {
             e.printStackTrace();
@@ -127,6 +207,7 @@
             } else {
                 set(key, value);
             }
+            redisTemplate.execute((RedisCallback<Void>) connection -> null);
             return true;
         } catch (Exception e) {
             e.printStackTrace();

--
Gitblit v1.9.1