From aeacfea53606a4aee0ce9b710f68a1123ee92ffb Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期二, 20 十二月 2022 10:33:10 +0800 Subject: [PATCH] !266 add 增加 RedisUtils 检查缓存对象是否存在方法 Merge pull request !266 from KonBAI/dev --- ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java | 129 +++++++++++++++++++++++++++++++++++------- 1 files changed, 106 insertions(+), 23 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java index aa5c0dc..1623808 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java @@ -1,17 +1,18 @@ package com.ruoyi.common.utils.redis; -import cn.hutool.core.collection.IterUtil; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.redisson.api.*; +import java.time.Duration; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * redis 宸ュ叿绫� @@ -100,14 +101,13 @@ * @since Redis 6.X 浠ヤ笂浣跨敤 setAndKeepTTL 鍏煎 5.X 鏂规 */ public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) { - RBucket<Object> bucket = CLIENT.getBucket(key); + RBucket<T> bucket = CLIENT.getBucket(key); if (isSaveTtl) { try { bucket.setAndKeepTTL(value); } catch (Exception e) { long timeToLive = bucket.remainTimeToLive(); - bucket.set(value); - bucket.expire(timeToLive, TimeUnit.MILLISECONDS); + setCacheObject(key, value, Duration.ofMillis(timeToLive)); } } else { bucket.set(value); @@ -119,18 +119,19 @@ * * @param key 缂撳瓨鐨勯敭鍊� * @param value 缂撳瓨鐨勫�� - * @param timeout 鏃堕棿 - * @param timeUnit 鏃堕棿棰楃矑搴� + * @param duration 鏃堕棿 */ - public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) { - RBucket<T> result = CLIENT.getBucket(key); - result.set(value); - result.expire(timeout, timeUnit); + public static <T> void setCacheObject(final String key, final T value, final Duration duration) { + RBatch batch = CLIENT.createBatch(); + RBucketAsync<T> bucket = batch.getBucket(key); + bucket.setAsync(value); + bucket.expireAsync(duration); + batch.execute(); } /** * 娉ㄥ唽瀵硅薄鐩戝惉鍣� - * + * <p> * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆 * * @param key 缂撳瓨鐨勯敭鍊� @@ -149,20 +150,19 @@ * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触 */ public static boolean expire(final String key, final long timeout) { - return expire(key, timeout, TimeUnit.SECONDS); + return expire(key, Duration.ofSeconds(timeout)); } /** * 璁剧疆鏈夋晥鏃堕棿 * - * @param key Redis閿� - * @param timeout 瓒呮椂鏃堕棿 - * @param unit 鏃堕棿鍗曚綅 + * @param key Redis閿� + * @param duration 瓒呮椂鏃堕棿 * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触 */ - public static boolean expire(final String key, final long timeout, final TimeUnit unit) { + public static boolean expire(final String key, final Duration duration) { RBucket rBucket = CLIENT.getBucket(key); - return rBucket.expire(timeout, unit); + return rBucket.expire(duration); } /** @@ -210,6 +210,15 @@ } /** + * 妫�鏌ョ紦瀛樺璞℃槸鍚﹀瓨鍦� + * + * @param key 缂撳瓨鐨勯敭鍊� + */ + public static boolean isExistsObject(final String key) { + return CLIENT.getBucket(key).isExists(); + } + + /** * 缂撳瓨List鏁版嵁 * * @param key 缂撳瓨鐨勯敭鍊� @@ -223,7 +232,7 @@ /** * 娉ㄥ唽List鐩戝惉鍣� - * + * <p> * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆 * * @param key 缂撳瓨鐨勯敭鍊� @@ -259,7 +268,7 @@ /** * 娉ㄥ唽Set鐩戝惉鍣� - * + * <p> * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆 * * @param key 缂撳瓨鐨勯敭鍊� @@ -296,7 +305,7 @@ /** * 娉ㄥ唽Map鐩戝惉鍣� - * + * <p> * key 鐩戝惉鍣ㄩ渶寮�鍚� `notify-keyspace-events` 绛� redis 鐩稿叧閰嶇疆 * * @param key 缂撳瓨鐨勯敭鍊� @@ -316,6 +325,17 @@ public static <T> Map<String, T> getCacheMap(final String key) { RMap<String, T> rMap = CLIENT.getMap(key); return rMap.getAll(rMap.keySet()); + } + + /** + * 鑾峰緱缂撳瓨Map鐨刱ey鍒楄〃 + * + * @param key 缂撳瓨鐨勯敭鍊� + * @return key鍒楄〃 + */ + public static <T> Set<String> getCacheMapKeySet(final String key) { + RMap<String, T> rMap = CLIENT.getMap(key); + return rMap.keySet(); } /** @@ -367,13 +387,76 @@ } /** + * 璁剧疆鍘熷瓙鍊� + * + * @param key Redis閿� + * @param value 鍊� + */ + public static void setAtomicValue(String key, long value) { + RAtomicLong atomic = CLIENT.getAtomicLong(key); + atomic.set(value); + } + + /** + * 鑾峰彇鍘熷瓙鍊� + * + * @param key Redis閿� + * @return 褰撳墠鍊� + */ + public static long getAtomicValue(String key) { + RAtomicLong atomic = CLIENT.getAtomicLong(key); + return atomic.get(); + } + + /** + * 閫掑鍘熷瓙鍊� + * + * @param key Redis閿� + * @return 褰撳墠鍊� + */ + public static long incrAtomicValue(String key) { + RAtomicLong atomic = CLIENT.getAtomicLong(key); + return atomic.incrementAndGet(); + } + + /** + * 閫掑噺鍘熷瓙鍊� + * + * @param key Redis閿� + * @return 褰撳墠鍊� + */ + public static long decrAtomicValue(String key) { + RAtomicLong atomic = CLIENT.getAtomicLong(key); + return atomic.decrementAndGet(); + } + + /** * 鑾峰緱缂撳瓨鐨勫熀鏈璞″垪琛� * * @param pattern 瀛楃涓插墠缂� * @return 瀵硅薄鍒楄〃 */ public static Collection<String> keys(final String pattern) { - Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern); - return IterUtil.toList(iterable); + Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern); + return stream.collect(Collectors.toList()); + } + + /** + * 鍒犻櫎缂撳瓨鐨勫熀鏈璞″垪琛� + * + * @param pattern 瀛楃涓插墠缂� + */ + public static void deleteKeys(final String pattern) { + CLIENT.getKeys().deleteByPattern(pattern); + } + + /** + * 妫�鏌edis涓槸鍚﹀瓨鍦╧ey + * + * @param key 閿� + */ + public static Boolean hasKey(String key) { + RKeys rKeys = CLIENT.getKeys(); + return rKeys.countExists(key) > 0; } } -- Gitblit v1.9.3