From c24d16a37523a49080392610357872ce580cbc38 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期一, 26 十月 2026 14:03:33 +0800
Subject: [PATCH] 代码提交

---
 ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java |   92 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 86 insertions(+), 6 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
index 6d48711..605ebc8 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
@@ -1,13 +1,11 @@
 package com.ruoyi.common.core.redis;
 
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.DataType;
 import org.springframework.data.redis.core.*;
 import org.springframework.stereotype.Component;
 
@@ -32,6 +30,10 @@
         redisTemplate.opsForValue().set(key, value);
     }
 
+    public <T> void setCacheList(final String key, final List<T> values) {
+        redisTemplate.opsForList().rightPushAll(key, values);
+    }
+
     /**
      * 缂撳瓨鍩烘湰鐨勫璞★紝Integer銆丼tring銆佸疄浣撶被绛�
      *
@@ -42,6 +44,23 @@
      */
     public <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
         redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
+    }
+
+    /**
+     * 浠呭綋 key 涓嶅瓨鍦ㄦ椂鍐欏叆锛圫ETNX 璇箟锛夛紝鐢ㄤ簬<b>骞傜瓑/闃查噸澶嶆彁浜�</b>
+     * <p>
+     * 2026-10-21 鏂板锛氶棶鍗锋彁浜ゆ帴鍙o紙{@code /smartor/subtaskAnswer/saveQuestionAnswer}锛夋槸
+     * {@code @Async} 寮傛澶勭悊锛屾偅鑰呯閲嶅鐐瑰嚮/缃戠粶閲嶈瘯浼氬苟鍙戝啓澶氫唤鏄庣粏锛岄渶瑕佺敤鍘熷瓙鍗犱綅鎸℃帀銆�
+     *
+     * @param key      Redis 閿�
+     * @param value    鍐欏叆鐨勫��
+     * @param timeout  杩囨湡鏃堕棿
+     * @param timeUnit 鏃堕棿棰楃矑搴�
+     * @return {@code true} = 鍗犱綅鎴愬姛锛堟湰娆″簲缁х画澶勭悊锛夛紱{@code false} = 宸插瓨鍦紙閲嶅璇锋眰锛屽簲蹇界暐锛�
+     */
+    public <T> boolean setCacheObjectIfAbsent(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
+        Boolean ok = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
+        return Boolean.TRUE.equals(ok);
     }
 
     /**
@@ -124,10 +143,48 @@
      * @param dataList 寰呯紦瀛樼殑List鏁版嵁
      * @return 缂撳瓨鐨勫璞�
      */
-    public <T> long setCacheList(final String key, final List<T> dataList) {
+    public <T> long setCacheListRight(final String key, final List<T> dataList) {
         Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
         return count == null ? 0 : count;
     }
+
+    /**
+     * 缂撳瓨List鏁版嵁
+     *
+     * @param key      缂撳瓨鐨勯敭鍊�
+     * @param dataList 寰呯紦瀛樼殑List鏁版嵁(杩欓噷濡傛灉鏂板鐨勮瘽锛屾槸寰�澶撮儴鏂板)
+     * @return 缂撳瓨鐨勫璞�
+     */
+    public <T> long setCacheListLeft(final String key, final List<T> dataList) {
+        Long count = redisTemplate.opsForList().leftPushAll(key, dataList);
+        return count == null ? 0 : count;
+    }
+
+    /**
+     * 缂撳瓨List鏁版嵁
+     *
+     * @param cacheKey 缂撳瓨鐨勯敭鍊�
+     * @param list     寰呯紦瀛樼殑List鏁版嵁(杩欓噷濡傛灉鏂板鐨勮瘽锛屾槸寰�澶撮儴鏂板)骞跺幓閲�
+     * @return 缂撳瓨鐨勫璞�
+     */
+    public void setCacheListLeftAndDistinct(String cacheKey, List<String> list) {
+        // 妫�鏌� key 绫诲瀷
+        DataType keyType = redisTemplate.type(cacheKey);
+        if (keyType != DataType.NONE && keyType != DataType.LIST) {
+            // 濡傛灉宸茬粡瀛樺湪浣嗕笉鏄� list 绫诲瀷锛屽垹闄ゅ畠锛堟垨鐩存帴鎶涘紓甯镐篃鍙級
+            redisTemplate.delete(cacheKey);
+        }
+
+        ListOperations<String, String> listOps = redisTemplate.opsForList();
+        List<String> existingValues = listOps.range(cacheKey, 0, -1);
+
+        List<String> newValues = list.stream().filter(value -> !existingValues.contains(value)).collect(Collectors.toList());
+
+        if (!newValues.isEmpty()) {
+            listOps.leftPushAll(cacheKey, newValues);
+        }
+    }
+
 
     /**
      * 鑾峰緱缂撳瓨鐨刲ist瀵硅薄
@@ -263,4 +320,27 @@
     public Collection<String> keys(final String pattern) {
         return redisTemplate.keys(pattern);
     }
+
+    /**
+     * 鑾峰彇褰撳墠key鍊肩殑鏁伴噺
+     * @param key
+     * @return
+     */
+    public Long getListSize(String key) {
+        // 妫�鏌� key 绫诲瀷
+        DataType keyType = redisTemplate.type(key);
+
+        // 濡傛灉 key 涓嶅瓨鍦紝杩斿洖 0
+        if (keyType == DataType.NONE) {
+            return 0L;
+        }
+
+        // 濡傛灉涓嶆槸 List 绫诲瀷锛屽垹闄ゅ畠骞惰繑鍥� 0锛堥槻姝� WRONGTYPE 閿欒锛�
+        if (keyType != DataType.LIST) {
+            redisTemplate.delete(key);
+            return 0L;
+        }
+
+        return redisTemplate.opsForList().size(key);
+    }
 }

--
Gitblit v1.9.3