refactor(cache/redisson): 移除 RedisQueueUtils 类

This commit is contained in:
2025-07-25 21:10:43 +08:00
parent 8676f9b591
commit e5354b765d
2 changed files with 69 additions and 210 deletions

View File

@@ -1,146 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 <T> RBlockingQueue<T> getBlockingQueue(String key) {
return CLIENT.getBlockingQueue(key);
}
/**
* 添加元素到阻塞队列尾部
* <p>
* 如果队列已满,则返回 false
* </p>
*
* @param key 键
* @param value 值
* @return true添加成功false添加失败
*/
public static <T> boolean addBlockingQueueData(String key, T value) {
RBlockingQueue<T> queue = getBlockingQueue(key);
return queue.offer(value);
}
/**
* 获取并移除阻塞队列头部元素
* <p>
* 如果队列为空,则返回 null
* </p>
*
* @param key 键
* @return 队列头部元素,如果队列为空,则返回 null
*/
public static <T> T getBlockingQueueData(String key) {
RBlockingQueue<T> queue = getBlockingQueue(key);
return queue.poll();
}
/**
* 删除阻塞队列中的指定元素
*
* @param key 键
* @param value 值
* @return true删除成功false删除失败
*/
public static <T> boolean removeBlockingQueueData(String key, T value) {
RBlockingQueue<T> queue = getBlockingQueue(key);
return queue.remove(value);
}
/**
* 获取延迟队列实例
*
* @param key 键
* @return 延迟队列实例
*/
public static <T> RDelayedQueue<T> getDelayedQueue(String key) {
RBlockingQueue<T> blockingQueue = getBlockingQueue(key);
return CLIENT.getDelayedQueue(blockingQueue);
}
/**
* 添加元素到延迟队列
*
* @param key 键
* @param value 值
* @param delay 延迟时间
* @param unit 时间单位
*/
public static <T> void addDelayedQueueData(String key, T value, long delay, TimeUnit unit) {
RDelayedQueue<T> delayedQueue = getDelayedQueue(key);
delayedQueue.offer(value, delay, unit);
}
/**
* 获取并移除延迟队列头部元素
* <p>
* 如果队列为空,则返回 null
* </p>
*
* @param key 键
* @return 队列头部元素,如果队列为空,则返回 null
*/
public static <T> T getDelayedQueueData(String key) {
RDelayedQueue<T> delayedQueue = getDelayedQueue(key);
return delayedQueue.poll();
}
/**
* 移除延迟队列中的指定元素
*
* @param key 键
* @param value 值
* @return true移除成功false移除失败
*/
public static <T> boolean removeDelayedQueueData(String key, T value) {
RDelayedQueue<T> delayedQueue = getDelayedQueue(key);
return delayedQueue.remove(value);
}
}

View File

@@ -263,6 +263,75 @@ public class RedisUtils {
return CLIENT.getKeys().getKeysStream(options).toList();
}
/**
* 设置 Hash 中指定字段的值
*
* @param key Hash 键
* @param field 字段
* @param value 值
* @author KAI
* @since 2.13.4
*/
public static <T> void hSet(String key, String field, T value) {
RMap<String, T> map = CLIENT.getMap(key);
map.put(field, value);
}
/**
* 获取 Hash 中指定字段的值
*
* @param key Hash 键
* @param field 字段
* @return 值
* @author KAI
* @since 2.13.4
*/
public static <T> T hGet(String key, String field) {
RMap<String, T> map = CLIENT.getMap(key);
return map.get(field);
}
/**
* 获取整个 Hash 的所有字段值
*
* @param key Hash 键
* @return Map
* @author KAI
* @since 2.13.4
*/
public static <T> Map<String, T> hGetAll(String key) {
RMap<String, T> map = CLIENT.getMap(key);
return map.readAllMap();
}
/**
* 判断 Hash 中是否存在指定字段
*
* @param key Hash 键
* @param field 字段
* @return true存在false不存在
* @author KAI
* @since 2.13.4
*/
public static boolean hExists(String key, String field) {
RMap<String, ?> map = CLIENT.getMap(key);
return map.containsKey(field);
}
/**
* 删除 Hash 中指定字段
*
* @param key Hash 键
* @param fields 字段数组
* @return 删除成功的字段数量
* @author KAI
* @since 2.13.4
*/
public static long hDel(String key, String... fields) {
RMap<String, ?> map = CLIENT.getMap(key);
return map.fastRemove(fields);
}
/**
* 添加元素到 ZSet 中
*
@@ -535,68 +604,4 @@ public class RedisUtils {
public static String formatKey(String... subKeys) {
return String.join(StringConstants.COLON, ArrayUtil.removeBlank(subKeys));
}
/**
* 设置 Hash 中指定字段的值
*
* @param key Hash 键
* @param field 字段
* @param value 值
* @since 2.13.4
*/
public static <T> void hSet(String key, String field, T value) {
RMap<String, T> map = CLIENT.getMap(key);
map.put(field, value);
}
/**
* 获取 Hash 中指定字段的值
*
* @param key Hash 键
* @param field 字段
* @return 值
* @since 2.13.4
*/
public static <T> T hGet(String key, String field) {
RMap<String, T> map = CLIENT.getMap(key);
return map.get(field);
}
/**
* 获取整个 Hash 的所有字段值
*
* @param key Hash 键
* @return Map
* @since 2.13.4
*/
public static <T> Map<String, T> hGetAll(String key) {
RMap<String, T> map = CLIENT.getMap(key);
return map.readAllMap();
}
/**
* 判断 Hash 中是否存在指定字段
*
* @param key Hash 键
* @param field 字段
* @return true存在false不存在
* @since 2.13.4
*/
public static boolean hExists(String key, String field) {
RMap<String, ?> map = CLIENT.getMap(key);
return map.containsKey(field);
}
/**
* 删除 Hash 中指定字段
*
* @param key Hash 键
* @param fields 字段数组
* @return 删除成功的字段数量
* @since 2.13.4
*/
public static long hDel(String key, String... fields) {
RMap<String, ?> map = CLIENT.getMap(key);
return map.fastRemove(fields);
}
}