|  |  | 
 |  |  | 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.connection.DataType; | 
 |  |  | import org.springframework.data.redis.core.*; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | 
 |  |  |      */ | 
 |  |  |     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); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * @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对象 | 
 |  |  | 
 |  |  |     public Collection<String> keys(final String pattern) { | 
 |  |  |         return redisTemplate.keys(pattern); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取当前key值的数量 | 
 |  |  |      * @param key | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public Long getListSize(String key) { | 
 |  |  |         return redisTemplate.opsForList().size(key); | 
 |  |  |     } | 
 |  |  | } |