From 56edceec7e7c61bbd06a1995c2351441302ac969 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 14 Nov 2024 20:24:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(cache/redisson):=20RedisUtils=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20ZSet=20=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/redisson/util/RedisUtils.java | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) 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; + } + /** * 限流 *