From 62dbd8169d8cad03e7a06f9b53b0a1928caf732f Mon Sep 17 00:00:00 2001 From: liusheng <337615773@qq.com> Date: 星期四, 06 三月 2025 18:13:45 +0800 Subject: [PATCH] 代码提交 --- ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java | 65 +++++++++++++++++++++++++++----- 1 files changed, 54 insertions(+), 11 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java index 13210c8..5eecedc 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java @@ -1,17 +1,11 @@ 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.core.*; import org.springframework.stereotype.Component; /** @@ -19,7 +13,7 @@ * * @author ruoyi **/ -@SuppressWarnings(value = {"unchecked" , "rawtypes"}) +@SuppressWarnings(value = {"unchecked", "rawtypes"}) @Component public class RedisCache { @Autowired @@ -127,10 +121,46 @@ * @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) { + // 鑾峰彇 ListOperations 瀵硅薄锛岀敤浜庢搷浣� Redis List + ListOperations<String, String> listOps = redisTemplate.opsForList(); + + // 鑾峰彇鐜版湁鐨� cache-0 鍒楄〃涓殑鎵�鏈夊�� + 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); + } + } + /** * 鑾峰緱缂撳瓨鐨刲ist瀵硅薄 @@ -245,6 +275,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 瀛楃涓插墠缂� -- Gitblit v1.9.3