forked from dyf/fys-Multi-tenant
web端控制中心4
This commit is contained in:
@ -11,6 +11,7 @@ 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;
|
||||
@ -373,6 +374,28 @@ public class RedisUtils {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间范围查询Sorted Set中的元素(重载方法,适用于时间戳查询)
|
||||
*
|
||||
* @param key 键
|
||||
* @param startTime 开始时间戳
|
||||
* @param endTime 结束时间戳
|
||||
* @return 指定时间范围内的元素集合
|
||||
*/
|
||||
public static Collection<String> zRangeByScore(String key, Long startTime, Long endTime) {
|
||||
try {
|
||||
RScoredSortedSet<String> sortedSet = CLIENT.getScoredSortedSet(key);
|
||||
return sortedSet.valueRange(startTime, true, endTime, true);
|
||||
} catch (Exception e) {
|
||||
// 记录错误日志(如果项目中有日志工具的话)
|
||||
// log.error("根据时间范围查询Sorted Set中的元素失败: key={}, startTime={}, endTime={}, error={}",
|
||||
// key, startTime, endTime, e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 追加缓存Set数据
|
||||
*
|
||||
@ -614,4 +637,73 @@ public class RedisUtils {
|
||||
RKeys rKeys = CLIENT.getKeys();
|
||||
return rKeys.countExists(key) > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 向去重阻塞队列中添加元素
|
||||
*
|
||||
* @param queueKey 队列键
|
||||
* @param dedupKey 去重集合键
|
||||
* @param value 消息值
|
||||
* @param timeout 过期时间
|
||||
* @return 是否添加成功
|
||||
*/
|
||||
public static boolean offerDeduplicated(String queueKey, String dedupKey, String value, Duration timeout) {
|
||||
// String jsonValue = value instanceof String ? (String) value : JsonUtils.toJsonString(value);
|
||||
|
||||
RLock lock = CLIENT.getLock("lock:" + queueKey);
|
||||
try {
|
||||
lock.lock();
|
||||
|
||||
RSet<String> dedupSet = CLIENT.getSet(dedupKey);
|
||||
if (dedupSet.contains(value)) {
|
||||
return false; // 元素已存在,不重复添加
|
||||
}
|
||||
|
||||
// 添加到去重集合
|
||||
dedupSet.add(value);
|
||||
|
||||
// 添加到阻塞队列
|
||||
RBlockingQueue<String> queue = CLIENT.getBlockingQueue(queueKey);
|
||||
boolean offered = queue.offer(value);
|
||||
|
||||
// 设置过期时间
|
||||
if (timeout != null) {
|
||||
queue.expire(timeout);
|
||||
dedupSet.expire(timeout);
|
||||
}
|
||||
|
||||
return offered;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从去重阻塞队列中消费元素
|
||||
*
|
||||
* @param queueKey 队列键
|
||||
* @param dedupKey 去重集合键
|
||||
* @param timeout 超时时间
|
||||
* @param timeUnit 时间单位
|
||||
* @return 消息值
|
||||
*/
|
||||
public static String pollDeduplicated(String queueKey, String dedupKey, long timeout, TimeUnit timeUnit) {
|
||||
try {
|
||||
RBlockingQueue<String> queue = CLIENT.getBlockingQueue(queueKey);
|
||||
String value = queue.poll(timeout, timeUnit);
|
||||
|
||||
// 从去重集合中移除
|
||||
if (value != null) {
|
||||
RSet<String> dedupSet = CLIENT.getSet(dedupKey);
|
||||
dedupSet.remove(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user