From 9cf3ae87a1a20db9ee8b2b7272e8328b5fc5c20c Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 18 Dec 2023 20:22:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(cache/redisson):=20RedisUtils=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E9=99=90=E6=B5=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/redisson/util/RedisUtils.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/util/RedisUtils.java b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/util/RedisUtils.java index d35e2ba1..3d2bb3cb 100644 --- a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/util/RedisUtils.java +++ b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/util/RedisUtils.java @@ -81,7 +81,7 @@ public class RedisUtils { * 删除缓存 * * @param key 键 - * @return true 设置成功;false 设置失败 + * @return true:设置成功;false:设置失败 */ public static boolean delete(final String key) { return CLIENT.getBucket(key).delete(); @@ -92,7 +92,7 @@ public class RedisUtils { * * @param key 键 * @param timeout 过期时间(单位:秒) - * @return true 设置成功;false 设置失败 + * @return true:设置成功;false:设置失败 */ public static boolean expire(final String key, final long timeout) { return expire(key, Duration.ofSeconds(timeout)); @@ -103,7 +103,7 @@ public class RedisUtils { * * @param key 键 * @param duration 过期时间 - * @return true 设置成功;false 设置失败 + * @return true:设置成功;false:设置失败 */ public static boolean expire(final String key, final Duration duration) { return CLIENT.getBucket(key).expire(duration); @@ -123,7 +123,7 @@ public class RedisUtils { * 是否存在指定缓存 * * @param key 键 - * @return true 存在;false 不存在 + * @return true:存在;false:不存在 */ public static boolean hasKey(String key) { RKeys keys = CLIENT.getKeys(); @@ -141,6 +141,21 @@ public class RedisUtils { return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList()); } + /** + * 限流 + * + * @param key 键 + * @param rateType 限流类型(OVERALL:全局限流;PER_CLIENT:单机限流) + * @param rate 速率(指定时间间隔产生的令牌数) + * @param rateInterval 速率间隔(时间间隔,单位:秒) + * @return true:成功;false:失败 + */ + public static boolean rateLimit(String key, RateType rateType, int rate, int rateInterval) { + RRateLimiter rateLimiter = CLIENT.getRateLimiter(key); + rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS); + return rateLimiter.tryAcquire(1); + } + /** * 格式化键,将各子键用 : 拼接起来 *