From 9dd748e0352f31b6255be2860c177db0d20ac40e Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 21 十月 2025 14:41:35 +0800
Subject: [PATCH] Merge branch 'master' into 0929-master
---
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java | 30 +++++++++++++++++++++++-------
1 files changed, 23 insertions(+), 7 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 5eecedc..79785fd 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
@@ -5,6 +5,7 @@
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;
@@ -27,6 +28,10 @@
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
+ }
+
+ public <T> void setCacheList(final String key, final List<T> values) {
+ redisTemplate.opsForList().rightPushAll(key, values);
}
/**
@@ -141,21 +146,23 @@
/**
* 缂撳瓨List鏁版嵁
*
- * @param cacheKey 缂撳瓨鐨勯敭鍊�
- * @param list 寰呯紦瀛樼殑List鏁版嵁(杩欓噷濡傛灉鏂板鐨勮瘽锛屾槸寰�澶撮儴鏂板)骞跺幓閲�
+ * @param cacheKey 缂撳瓨鐨勯敭鍊�
+ * @param list 寰呯紦瀛樼殑List鏁版嵁(杩欓噷濡傛灉鏂板鐨勮瘽锛屾槸寰�澶撮儴鏂板)骞跺幓閲�
* @return 缂撳瓨鐨勫璞�
*/
public void setCacheListLeftAndDistinct(String cacheKey, List<String> list) {
- // 鑾峰彇 ListOperations 瀵硅薄锛岀敤浜庢搷浣� Redis List
- ListOperations<String, String> listOps = redisTemplate.opsForList();
+ // 妫�鏌� key 绫诲瀷
+ DataType keyType = redisTemplate.type(cacheKey);
+ if (keyType != DataType.NONE && keyType != DataType.LIST) {
+ // 濡傛灉宸茬粡瀛樺湪浣嗕笉鏄� list 绫诲瀷锛屽垹闄ゅ畠锛堟垨鐩存帴鎶涘紓甯镐篃鍙級
+ redisTemplate.delete(cacheKey);
+ }
- // 鑾峰彇鐜版湁鐨� cache-0 鍒楄〃涓殑鎵�鏈夊��
+ ListOperations<String, String> listOps = redisTemplate.opsForList();
List<String> existingValues = listOps.range(cacheKey, 0, -1);
- // 杩囨护鍑轰笉鍦� Redis 鍒楄〃涓殑鍊�
List<String> newValues = list.stream().filter(value -> !existingValues.contains(value)).collect(Collectors.toList());
- // 灏嗘柊鐨勫�兼坊鍔犲埌 Redis 鍒楄〃鐨勫乏渚�
if (!newValues.isEmpty()) {
listOps.leftPushAll(cacheKey, newValues);
}
@@ -296,4 +303,13 @@
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
+
+ /**
+ * 鑾峰彇褰撳墠key鍊肩殑鏁伴噺
+ * @param key
+ * @return
+ */
+ public Long getListSize(String key) {
+ return redisTemplate.opsForList().size(key);
+ }
}
--
Gitblit v1.9.3