From 18f876d637cf1e9b0b264a4586e4a676a21f8166 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期五, 13 十一月 2026 10:30:46 +0800
Subject: [PATCH] 代码提交
---
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java | 69 +++++++++++++++++++++++++++++++++-
1 files changed, 66 insertions(+), 3 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 8955e98..5ea0792 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
@@ -4,7 +4,9 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
@@ -14,6 +16,7 @@
*
* @author ruoyi
**/
+@Slf4j
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
@@ -30,6 +33,10 @@
redisTemplate.opsForValue().set(key, value);
}
+ public <T> void setCacheList(final String key, final List<T> values) {
+ redisTemplate.opsForList().rightPushAll(key, values);
+ }
+
/**
* 缂撳瓨鍩烘湰鐨勫璞★紝Integer銆丼tring銆佸疄浣撶被绛�
*
@@ -40,6 +47,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);
}
/**
@@ -285,9 +309,25 @@
* @return Long 澶т簬0琛ㄧず鍒犻櫎浜嗗嚑涓紝绛変簬0琛ㄧず娌℃湁鍒犻櫎锛屽皬浜�0琛ㄧず鍦ㄦ墽琛屽垹闄ゆ搷浣滄椂鍙戠敓浜嗛敊璇�
*/
public Long removeElementFromList(String key, String valueToRemove) {
- ListOperations<String, String> listOps = redisTemplate.opsForList();
- Long remove = listOps.remove(key, 0, valueToRemove);
- return remove;
+ // 鍏堝垽鏂� key 鐨勭被鍨嬶紝閬垮厤 key 琚叾瀹冧笟鍔�/鏃х増鏈唬鐮佸啓鎴愪簡 String銆丠ash 绛夐潪 List 绫诲瀷鏃舵姏 WRONGTYPE
+ DataType keyType = redisTemplate.type(key);
+ if (keyType == DataType.NONE) {
+ return 0L;
+ }
+ if (keyType != DataType.LIST) {
+ // 璇� key 鏈韩鏄綔涓洪槦鍒椾娇鐢ㄧ殑锛岀被鍨嬩笉瀵硅鏄庢槸鑴忔暟鎹紝鐩存帴鍒犻櫎锛岀瓑涓嬫鍐欏叆鏃堕噸鏂板垱寤轰负 List
+ log.warn("Redis key [{}] 鐨勭被鍨嬩负 {}锛屼笉鏄� List锛屽凡鍒犻櫎璇ヨ剰鏁版嵁锛岄伩鍏� WRONGTYPE 寮傚父", key, keyType);
+ redisTemplate.delete(key);
+ return 0L;
+ }
+ try {
+ Long remove = redisTemplate.opsForList().remove(key, 0, valueToRemove);
+ return remove == null ? 0L : remove;
+ } catch (DataAccessException e) {
+ // 绫诲瀷妫�鏌ュ拰鍒犻櫎涔嬮棿 key 琚叾瀹冨簲鐢ㄦ敼鍐欎负鍏跺畠绫诲瀷绛夋瀬绔儏鍐靛厹搴曪紝缂撳瓨娓呯悊澶辫触涓嶅簲褰卞搷涓讳笟鍔℃祦绋�
+ log.warn("浠� Redis List [{}] 绉婚櫎鍏冪礌 [{}] 澶辫触锛歿}", key, valueToRemove, e.getMessage());
+ return 0L;
+ }
}
/**
@@ -299,4 +339,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