跳转到内容

ZSCORE

ZSCORE 命令返回有序集合中指定成员的分数。如果成员不存在,则返回 nil。

ZSCORE 命令的时间复杂度为 O(1)。

Terminal window
ZSCORE key member

参数说明

  • key: 有序集合的键
  • member: 成员名称

ZSCORE 命令用于获取有序集合中指定成员的分数。如果成员不存在于有序集合中,则返回 nil。如果键不存在,也会返回 nil。

  • 如果成员存在,返回其分数(字符串形式)
  • 如果成员不存在或键不存在,返回 nil
Terminal window
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZSCORE myzset "one"
"1"
redis> ZSCORE myzset "two"
(nil)

在 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");
}
});
  1. 如果成员不存在,方法返回 null
  2. 如果键不存在,方法也返回 null
  3. 分数以 Double 类型返回