From 896cbe067484143f7b23e28ac7c2a2e395f1cb95 Mon Sep 17 00:00:00 2001 From: Lemon Date: Thu, 7 Nov 2024 14:14:11 +0800 Subject: [PATCH] =?UTF-8?q?redisCache=20=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E4=B8=AD=E6=96=B0=E5=A2=9E=20=20=E6=89=B9=E9=87=8F=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0list=E9=9B=86=E5=90=88=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jsowell/common/core/redis/RedisCache.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java b/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java index b2987c5d7..2af1cd5d0 100644 --- a/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java +++ b/jsowell-common/src/main/java/com/jsowell/common/core/redis/RedisCache.java @@ -1,6 +1,7 @@ package com.jsowell.common.core.redis; import com.jsowell.common.util.StringUtils; +import org.apache.poi.ss.formula.functions.T; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -209,6 +210,33 @@ public class RedisCache { return count == null ? 0 : count; } + /** + * 批量存储 List 集合到 Redis,并设置缓存超时时间 + * + * @param map 其中 map 的 key 为缓存键值,value 为 List 集合 + * @param timeout 缓存超时时间 + * @param unit 超时时间的单位(如 TimeUnit.SECONDS、TimeUnit.MINUTES) + * @return 存储成功的元素总数 + */ + public long batchSetCacheList(final Map> map, long timeout, TimeUnit unit) { + long totalCount = 0; + + for (Map.Entry> entry : map.entrySet()) { + String key = entry.getKey(); + List dataList = entry.getValue(); + + if (dataList != null && !dataList.isEmpty()) { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + totalCount += (count != null ? count : 0); + + // 设置缓存超时时间 + redisTemplate.expire(key, timeout, unit); + } + } + + return totalCount; + } + /** * 获得缓存的list对象 *