ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
@@ -4,7 +4,10 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
@@ -13,6 +16,7 @@
 *
 * @author ruoyi
 **/
@Slf4j
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
@@ -27,6 +31,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 +149,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);
        }
@@ -282,9 +292,25 @@
     * @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;
        // 先判断 key 的类型,避免 key 被其它业务/旧版本代码写成了 String、Hash 等非 List 类型时抛 WRONGTYPE
        DataType keyType = redisTemplate.type(key);
        if (keyType == DataType.NONE) {
            return 0L;
        }
        if (keyType != DataType.LIST) {
            // 该 key 本身是作为队列使用的,类型不对说明是脏数据,直接删除,等下次写入时重新创建为 List
            log.warn("Redis key [{}] 的类型为 {},不是 List,已删除该脏数据,避免 WRONGTYPE 异常", key, keyType);
            redisTemplate.delete(key);
            return 0L;
        }
        try {
            Long remove = redisTemplate.opsForList().remove(key, 0, valueToRemove);
            return remove == null ? 0L : remove;
        } catch (DataAccessException e) {
            // 类型检查和删除之间 key 被其它应用改写为其它类型等极端情况兜底,缓存清理失败不应影响主业务流程
            log.warn("从 Redis List [{}] 移除元素 [{}] 失败:{}", key, valueToRemove, e.getMessage());
            return 0L;
        }
    }
    /**
@@ -296,4 +322,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);
    }
}