From c97c04770c17c36c554963bf8bb8d8fafc6a8d43 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 07 五月 2026 09:43:35 +0800
Subject: [PATCH] #地图更新刷新缓存

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

diff --git a/src/main/java/com/zy/common/utils/RedisUtil.java b/src/main/java/com/zy/common/utils/RedisUtil.java
index a51936c..6a28925 100644
--- a/src/main/java/com/zy/common/utils/RedisUtil.java
+++ b/src/main/java/com/zy/common/utils/RedisUtil.java
@@ -4,9 +4,11 @@
 import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.data.redis.core.script.DefaultRedisScript;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -116,6 +118,36 @@
         });
     }
 
+    public int deleteByPrefix(String keyPrefix) {
+        if (keyPrefix == null || keyPrefix.trim().isEmpty()) {
+            return 0;
+        }
+        Integer deletedCount = (Integer) redisTemplate.execute((org.springframework.data.redis.core.RedisCallback<Integer>) connection -> {
+            org.springframework.data.redis.core.ScanOptions options = org.springframework.data.redis.core.ScanOptions.scanOptions()
+                    .match(keyPrefix + "*")
+                    .count(1000)
+                    .build();
+            org.springframework.data.redis.core.Cursor<byte[]> cursor = connection.scan(options);
+            int count = 0;
+            try {
+                while (cursor.hasNext()) {
+                    byte[] key = cursor.next();
+                    Long deleted = connection.keyCommands().del(key);
+                    if (deleted != null) {
+                        count += deleted.intValue();
+                    }
+                }
+            } finally {
+                try {
+                    cursor.close();
+                } catch (Exception e) {
+                }
+            }
+            return count;
+        });
+        return deletedCount == null ? 0 : deletedCount;
+    }
+
     public java.util.List<Object> multiGet(java.util.Collection<String> keys) {
         return redisTemplate.opsForValue().multiGet(keys);
     }
@@ -209,6 +241,44 @@
             }
             redisTemplate.execute((RedisCallback<Void>) connection -> null);
             return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    public boolean trySetStringIfAbsent(String key, String value, long timeSeconds) {
+        if (key == null || value == null) {
+            return false;
+        }
+        try {
+            Boolean result;
+            if (timeSeconds > 0) {
+                result = stringRedisTemplate.opsForValue().setIfAbsent(key, value, timeSeconds, TimeUnit.SECONDS);
+            } else {
+                result = stringRedisTemplate.opsForValue().setIfAbsent(key, value);
+            }
+            return Boolean.TRUE.equals(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    public boolean compareAndDelete(String key, String expectedValue) {
+        if (key == null || expectedValue == null) {
+            return false;
+        }
+        try {
+            DefaultRedisScript<Long> script = new DefaultRedisScript<>();
+            script.setScriptText(
+                    "if redis.call('get', KEYS[1]) == ARGV[1] then " +
+                            "return redis.call('del', KEYS[1]) " +
+                            "else return 0 end"
+            );
+            script.setResultType(Long.class);
+            Long result = stringRedisTemplate.execute(script, Collections.singletonList(key), expectedValue);
+            return result != null && result > 0;
         } catch (Exception e) {
             e.printStackTrace();
             return false;
@@ -705,4 +775,3 @@
 
 
 }
-

--
Gitblit v1.9.1