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 a4beefd3..c7b04510 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 @@ -194,6 +194,167 @@ public class RedisUtils { return CLIENT.getKeys().getKeysStreamByPattern(pattern).toList(); } + /** + * 添加元素到 ZSet 中 + * + * @param key 键 + * @param value 值 + * @param score 分数 + * @return true:添加成功;false:添加失败 + * @since 2.7.3 + */ + public static boolean zAdd(String key, T value, double score) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.add(score, value); + } + + /** + * 查询 ZSet 中指定元素的分数 + * + * @param key 键 + * @param value 值 + * @return 分数(null 表示元素不存在) + * @since 2.7.3 + */ + public static Double zScore(String key, T value) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.getScore(value); + } + + /** + * 查询 ZSet 中指定元素的排名 + * + * @param key 键 + * @param value 值 + * @return 排名(从 0 开始,null 表示元素不存在) + * @since 2.7.3 + */ + public static Integer zRank(String key, T value) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.rank(value); + } + + /** + * 查询 ZSet 中的元素个数 + * + * @param key 键 + * @return 元素个数 + * @since 2.7.3 + */ + public static int zSize(String key) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.size(); + } + + /** + * 从 ZSet 中删除指定元素 + * + * @param key 键 + * @param value 值 + * @return true:删除成功;false:删除失败 + * @since 2.7.3 + */ + public static boolean zRemove(String key, T value) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.remove(value); + } + + /** + * 删除 ZSet 中指定分数范围内的元素 + * + * @param key 键 + * @param min 最小分数(包含) + * @param max 最大分数(包含) + * @return 删除的元素个数 + * @since 2.7.3 + */ + public static int zRemoveRangeByScore(String key, double min, double max) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.removeRangeByScore(min, true, max, true); + } + + /** + * 删除 ZSet 中指定排名范围内的元素 + * + *

+ * 索引从 0 开始。-1 表示最高分,-2 表示第二高分。 + *

+ * + * @param key 键 + * @param startIndex 起始索引 + * @param endIndex 结束索引 + * @return 删除的元素个数 + * @since 2.7.3 + */ + public static int zRemoveRangeByRank(String key, int startIndex, int endIndex) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.removeRangeByRank(startIndex, endIndex); + } + + /** + * 根据分数范围查询 ZSet 中的元素列表 + * + * @param key 键 + * @param min 最小分数(包含) + * @param max 最大分数(包含) + * @return 元素列表 + * @since 2.7.3 + */ + public static Collection zRangeByScore(String key, double min, double max) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.valueRange(min, true, max, true); + } + + /** + * 根据分数范围查询 ZSet 中的元素列表 + * + * @param key 键 + * @param min 最小分数(包含) + * @param max 最大分数(包含) + * @param offset 偏移量 + * @param count 数量 + * @return 元素列表 + * @since 2.7.3 + */ + public static Collection zRangeByScore(String key, double min, double max, int offset, int count) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.valueRange(min, true, max, true, offset, count); + } + + /** + * 根据分数范围查询 ZSet 中的元素个数 + * + * @param key 键 + * @param min 最小分数(包含) + * @param max 最大分数(包含) + * @return 元素个数 + * @since 2.7.3 + */ + public static int zCountRangeByScore(String key, double min, double max) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + return zSet.count(min, true, max, true); + } + + /** + * 计算 ZSet 中多个元素的分数之和 + * + * @param key 键 + * @param values 值列表 + * @return 分数之和 + * @since 2.7.3 + */ + public static double zSum(String key, Collection values) { + RScoredSortedSet zSet = CLIENT.getScoredSortedSet(key); + double sum = 0; + for (T value : values) { + Double score = zSet.getScore(value); + if (score != null) { + sum += score; + } + } + return sum; + } + /** * 限流 *