redisCache 工具类中新增 批量添加list集合方法

This commit is contained in:
Lemon
2024-11-07 14:14:11 +08:00
parent bad30f2847
commit 896cbe0674

View File

@@ -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 <T> long batchSetCacheList(final Map<String, List<T>> map, long timeout, TimeUnit unit) {
long totalCount = 0;
for (Map.Entry<String, List<T>> 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对象
*