From cff4f02d966836fd291c268d0e44b0047e35d053 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 26 Jul 2025 23:12:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cache/redisson):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=20RedisUtils=20=E4=B8=AD=E7=9A=84=20Lock=20=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95=EF=BC=88=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20RedisLockUtils=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/redisson/util/RedisLockUtils.java | 3 +- .../cache/redisson/util/RedisUtils.java | 124 ++++-------------- 2 files changed, 29 insertions(+), 98 deletions(-) diff --git a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisLockUtils.java b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisLockUtils.java index 92d48e03..2768e073 100644 --- a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisLockUtils.java +++ b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisLockUtils.java @@ -25,9 +25,10 @@ import top.continew.starter.core.util.SpringUtils; import java.util.concurrent.TimeUnit; /** - * Redisson分布式锁 工具类 + * Redisson 分布式锁工具类 * * @author lishuyan + * @since 2.13.4 */ public class RedisLockUtils implements AutoCloseable { diff --git a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisUtils.java b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisUtils.java index 335e06ad..b5f3d873 100644 --- a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisUtils.java +++ b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisUtils.java @@ -26,8 +26,6 @@ import java.time.Duration; import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; /** @@ -52,42 +50,6 @@ public class RedisUtils { return CLIENT; } - /** - * 发布消息 - * - * @param name 主题名称 - * @param msg 发送数据 - * @param consumer 自定义处理 - */ - public static void publish(String name, T msg, Consumer consumer) { - RTopic topic = CLIENT.getTopic(name); - topic.publish(msg); - consumer.accept(msg); - } - - /** - * 发布消息 - * - * @param name 主题名称 - * @param msg 发送数据 - */ - public static void publish(String name, T msg) { - RTopic topic = CLIENT.getTopic(name); - topic.publish(msg); - } - - /** - * 订阅消息 - * - * @param name 主题名称 - * @param clazz 消息类型 - * @param consumer 自定义处理 - */ - public static void subscribe(String name, Class clazz, Consumer consumer) { - RTopic topic = CLIENT.getTopic(name); - topic.addListener(clazz, (channel, msg) -> consumer.accept(msg)); - } - /** * 设置缓存 * @@ -559,77 +521,45 @@ public class RedisUtils { } /** - * 尝试获取锁 + * 发布消息 * - * @param key 键 - * @param expireTime 锁过期时间(单位:毫秒) - * @param timeout 获取锁超时时间(单位:毫秒) - * @return true:成功;false:失败 - * @since 2.7.2 + * @param name 主题名称 + * @param msg 发送数据 + * @param consumer 自定义处理 + * @author lishuyan + * @since 2.13.4 */ - public static boolean tryLock(String key, long expireTime, long timeout) { - return tryLock(key, expireTime, timeout, TimeUnit.MILLISECONDS); + public static void publish(String name, T msg, Consumer consumer) { + RTopic topic = CLIENT.getTopic(name); + topic.publish(msg); + consumer.accept(msg); } /** - * 释放锁 + * 发布消息 * - * @param key 键 - * @return true:释放成功;false:释放失败 - * @since 2.7.2 + * @param name 主题名称 + * @param msg 发送数据 + * @author lishuyan + * @since 2.13.4 */ - public static boolean unlock(String key) { - RLock lock = getLock(key); - return unlock(lock); + public static void publish(String name, T msg) { + RTopic topic = CLIENT.getTopic(name); + topic.publish(msg); } /** - * 尝试获取锁 + * 订阅消息 * - * @param key 键 - * @param expireTime 锁过期时间 - * @param timeout 获取锁超时时间 - * @param unit 时间单位 - * @return true:成功;false:失败 - * @since 2.7.2 + * @param name 主题名称 + * @param clazz 消息类型 + * @param consumer 自定义处理 + * @author lishuyan + * @since 2.13.4 */ - public static boolean tryLock(String key, long expireTime, long timeout, TimeUnit unit) { - RLock lock = getLock(key); - try { - return lock.tryLock(timeout, expireTime, unit); - } catch (InterruptedException e) { - return false; - } - } - - /** - * 释放锁 - * - * @param lock 锁实例 - * @return true:释放成功;false:释放失败 - * @since 2.7.2 - */ - public static boolean unlock(RLock lock) { - if (lock.isHeldByCurrentThread()) { - try { - lock.unlockAsync().get(); - return true; - } catch (ExecutionException | InterruptedException e) { - return false; - } - } - return false; - } - - /** - * 获取锁实例 - * - * @param key 键 - * @return 锁实例 - * @since 2.7.2 - */ - public static RLock getLock(String key) { - return CLIENT.getLock(key); + public static void subscribe(String name, Class clazz, Consumer consumer) { + RTopic topic = CLIENT.getTopic(name); + topic.addListener(clazz, (channel, msg) -> consumer.accept(msg)); } /**