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 | 31 +++++++++++++++++++++++++++++++
1 files changed, 31 insertions(+), 0 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 79785fd..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
@@ -47,6 +47,23 @@
}
/**
+ * 浠呭綋 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);
+ }
+
+ /**
* 璁剧疆鏈夋晥鏃堕棿
*
* @param key Redis閿�
@@ -310,6 +327,20 @@
* @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