From 3e28500e76ffd71b7da90f40d1f3126c00a0e5f0 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 13 四月 2026 14:49:41 +0800
Subject: [PATCH] #站点运行优化

---
 src/main/java/com/zy/common/utils/RedisUtil.java |   72 +++++++++++++++++++++++++++++++++++
 1 files changed, 71 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 76ad76d..2c27c76 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;
@@ -207,6 +209,75 @@
             } else {
                 set(key, value);
             }
+            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;
+        }
+    }
+
+    public boolean multiSet(Map<String, Object> values, long time) {
+        if (values == null || values.isEmpty()) {
+            return true;
+        }
+        try {
+            redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
+                for (Map.Entry<String, Object> entry : values.entrySet()) {
+                    if (entry == null || entry.getKey() == null) {
+                        continue;
+                    }
+                    byte[] keyBytes = redisTemplate.getStringSerializer().serialize(entry.getKey());
+                    byte[] valueBytes = redisTemplate.getValueSerializer().serialize(entry.getValue());
+                    if (keyBytes == null || valueBytes == null) {
+                        continue;
+                    }
+                    if (time > 0) {
+                        connection.stringCommands().setEx(keyBytes, time, valueBytes);
+                    } else {
+                        connection.stringCommands().set(keyBytes, valueBytes);
+                    }
+                }
+                return null;
+            });
             redisTemplate.execute((RedisCallback<Void>) connection -> null);
             return true;
         } catch (Exception e) {
@@ -674,4 +745,3 @@
 
 
 }
-

--
Gitblit v1.9.1