From c08b57cb489e29b44ae99ec5bd725b72ec9a83a3 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 9 Jun 2025 19:29:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(cache/redisson):=20=E6=96=B0=E5=A2=9E=20Re?= =?UTF-8?q?disQueueUtils=20=E9=98=9F=E5=88=97=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/redisson/util/RedisQueueUtils.java | 146 ++++++++++++++++++ .../cache/redisson/util/RedisUtils.java | 9 ++ 2 files changed, 155 insertions(+) create mode 100644 continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisQueueUtils.java diff --git a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisQueueUtils.java b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisQueueUtils.java new file mode 100644 index 00000000..d9226fcb --- /dev/null +++ b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/continew/starter/cache/redisson/util/RedisQueueUtils.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.cache.redisson.util; + +import cn.hutool.extra.spring.SpringUtil; +import org.redisson.api.*; + +import java.util.concurrent.TimeUnit; + +/** + * Redis 队列工具类 + * + * @author Charles7c + * @since 2.12.1 + */ +public class RedisQueueUtils { + + private static final RedissonClient CLIENT = SpringUtil.getBean(RedissonClient.class); + + private RedisQueueUtils() { + } + + /** + * 获取 Redisson 客户端实例 + * + * @return Redisson 客户端实例 + */ + public static RedissonClient getClient() { + return CLIENT; + } + + /** + * 获取阻塞队列实例 + * + * @param key 键 + * @return 阻塞队列实例 + */ + public static RBlockingQueue getBlockingQueue(String key) { + return CLIENT.getBlockingQueue(key); + } + + /** + * 添加元素到阻塞队列尾部 + *

+ * 如果队列已满,则返回 false + *

+ * + * @param key 键 + * @param value 值 + * @return true:添加成功;false:添加失败 + */ + public static boolean addBlockingQueueData(String key, T value) { + RBlockingQueue queue = getBlockingQueue(key); + return queue.offer(value); + } + + /** + * 获取并移除阻塞队列头部元素 + *

+ * 如果队列为空,则返回 null + *

+ * + * @param key 键 + * @return 队列头部元素,如果队列为空,则返回 null + */ + public static T getBlockingQueueData(String key) { + RBlockingQueue queue = getBlockingQueue(key); + return queue.poll(); + } + + /** + * 删除阻塞队列中的指定元素 + * + * @param key 键 + * @param value 值 + * @return true:删除成功;false:删除失败 + */ + public static boolean removeBlockingQueueData(String key, T value) { + RBlockingQueue queue = getBlockingQueue(key); + return queue.remove(value); + } + + /** + * 获取延迟队列实例 + * + * @param key 键 + * @return 延迟队列实例 + */ + public static RDelayedQueue getDelayedQueue(String key) { + RBlockingQueue blockingQueue = getBlockingQueue(key); + return CLIENT.getDelayedQueue(blockingQueue); + } + + /** + * 添加元素到延迟队列 + * + * @param key 键 + * @param value 值 + * @param delay 延迟时间 + * @param unit 时间单位 + */ + public static void addDelayedQueueData(String key, T value, long delay, TimeUnit unit) { + RDelayedQueue delayedQueue = getDelayedQueue(key); + delayedQueue.offer(value, delay, unit); + } + + /** + * 获取并移除延迟队列头部元素 + *

+ * 如果队列为空,则返回 null + *

+ * + * @param key 键 + * @return 队列头部元素,如果队列为空,则返回 null + */ + public static T getDelayedQueueData(String key) { + RDelayedQueue delayedQueue = getDelayedQueue(key); + return delayedQueue.poll(); + } + + /** + * 移除延迟队列中的指定元素 + * + * @param key 键 + * @param value 值 + * @return true:移除成功;false:移除失败 + */ + public static boolean removeDelayedQueueData(String key, T value) { + RDelayedQueue delayedQueue = getDelayedQueue(key); + return delayedQueue.remove(value); + } +} 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 5e9cb6a2..a3ab2479 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 @@ -41,6 +41,15 @@ public class RedisUtils { private RedisUtils() { } + /** + * 获取 Redisson 客户端实例 + * + * @return Redisson 客户端实例 + */ + public static RedissonClient getClient() { + return CLIENT; + } + /** * 设置缓存 *