liusheng
11 小时以前 c24d16a37523a49080392610357872ce580cbc38
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
@@ -1,17 +1,12 @@
package com.ruoyi.common.core.redis;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
/**
@@ -19,7 +14,7 @@
 *
 * @author ruoyi
 **/
@SuppressWarnings(value = {"unchecked" , "rawtypes"})
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
    @Autowired
@@ -35,6 +30,10 @@
        redisTemplate.opsForValue().set(key, value);
    }
    public <T> void setCacheList(final String key, final List<T> values) {
        redisTemplate.opsForList().rightPushAll(key, values);
    }
    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
@@ -45,6 +44,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 不存在时写入(SETNX 语义),用于<b>幂等/防重复提交</b>
     * <p>
     * 2026-10-21 新增:问卷提交接口({@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);
    }
    /**
@@ -127,10 +143,48 @@
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    public <T> long setCacheList(final String key, final List<T> dataList) {
    public <T> long setCacheListRight(final String key, final List<T> dataList) {
        Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0 : count;
    }
    /**
     * 缓存List数据
     *
     * @param key      缓存的键值
     * @param dataList 待缓存的List数据(这里如果新增的话,是往头部新增)
     * @return 缓存的对象
     */
    public <T> long setCacheListLeft(final String key, final List<T> dataList) {
        Long count = redisTemplate.opsForList().leftPushAll(key, dataList);
        return count == null ? 0 : count;
    }
    /**
     * 缓存List数据
     *
     * @param cacheKey 缓存的键值
     * @param list     待缓存的List数据(这里如果新增的话,是往头部新增)并去重
     * @return 缓存的对象
     */
    public void setCacheListLeftAndDistinct(String cacheKey, List<String> list) {
        // 检查 key 类型
        DataType keyType = redisTemplate.type(cacheKey);
        if (keyType != DataType.NONE && keyType != DataType.LIST) {
            // 如果已经存在但不是 list 类型,删除它(或直接抛异常也可)
            redisTemplate.delete(cacheKey);
        }
        ListOperations<String, String> listOps = redisTemplate.opsForList();
        List<String> existingValues = listOps.range(cacheKey, 0, -1);
        List<String> newValues = list.stream().filter(value -> !existingValues.contains(value)).collect(Collectors.toList());
        if (!newValues.isEmpty()) {
            listOps.leftPushAll(cacheKey, newValues);
        }
    }
    /**
     * 获得缓存的list对象
@@ -245,6 +299,19 @@
    }
    /**
     * 删除缓存LIST中的某个数据
     *
     * @param key           Redis键
     * @param valueToRemove 需要删除的值
     * @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;
    }
    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
@@ -253,4 +320,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);
    }
}