ZSCORE
ZSCORE 命令返回有序集合中指定成员的分数。如果成员不存在,则返回 nil。
ZSCORE 命令的时间复杂度为 O(1)。
Redis 原生命令语法
Section titled “Redis 原生命令语法”ZSCORE key member参数说明
- key: 有序集合的键
- member: 成员名称
ZSCORE 命令用于获取有序集合中指定成员的分数。如果成员不存在于有序集合中,则返回 nil。如果键不存在,也会返回 nil。
- 如果成员存在,返回其分数(字符串形式)
- 如果成员不存在或键不存在,返回 nil
redis> ZADD myzset 1 "one"(integer) 1redis> ZSCORE myzset "one""1"redis> ZSCORE myzset "two"(nil)Redisun 使用方式
Section titled “Redisun 使用方式”在 redisun 中,ZSCORE 命令通过 ZScoreCommand 类和 Redisun 类中的 zscore 方法实现。
Redisun redisun = Redisun.create(options -> { options.setHost("localhost"); options.setPort(6379);});
// 添加一些测试数据redisun.zadd("myzset", 1.0, "one");redisun.zadd("myzset", 2.5, "two");
// 获取成员的分数Double score = redisun.zscore("myzset", "one");System.out.println("Score of 'one': " + score); // 输出: Score of 'one': 1.0
// 获取不存在成员的分数Double nonExistentScore = redisun.zscore("myzset", "three");System.out.println("Score of 'three': " + nonExistentScore); // 输出: Score of 'three': null// 异步获取成员的分数CompletableFuture<Double> future = redisun.asyncZscore("myzset", "one");
// 处理异步结果future.thenAccept(score -> { if (score != null) { System.out.println("Score: " + score); } else { System.out.println("Member not found"); }});- 如果成员不存在,方法返回 null
- 如果键不存在,方法也返回 null
- 分数以 Double 类型返回