liusheng
2 天以前 1e1814178edf84090b28492bbad340ab13e5c169
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;
@@ -141,21 +142,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);
        }