Skip to content

ZSCORE

The ZSCORE command returns the score of a specified member in a sorted set. If the member does not exist, nil is returned.

The time complexity of the ZSCORE command is O(1).

Terminal window
ZSCORE key member

Parameter Description

  • key: The key of the sorted set
  • member: The name of the member

The ZSCORE command is used to get the score of a specified member in a sorted set. If the member does not exist in the sorted set, nil is returned. If the key does not exist, nil is also returned.

  • If the member exists, return its score (in string form)
  • If the member does not exist or the key does not exist, return nil
Terminal window
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZSCORE myzset "one"
"1"
redis> ZSCORE myzset "two"
(nil)

In redisun, the ZSCORE command is implemented through the ZScoreCommand class and the zscore method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setHost("localhost");
options.setPort(6379);
});
// Add some test data
redisun.zadd("myzset", 1.0, "one");
redisun.zadd("myzset", 2.5, "two");
// Get the score of a member
Double score = redisun.zscore("myzset", "one");
System.out.println("Score of 'one': " + score); // Output: Score of 'one': 1.0
// Get the score of a non-existent member
Double nonExistentScore = redisun.zscore("myzset", "three");
System.out.println("Score of 'three': " + nonExistentScore); // Output: Score of 'three': null
// Asynchronously get the score of a member
CompletableFuture<Double> future = redisun.asyncZscore("myzset", "one");
// Handle asynchronous result
future.thenAccept(score -> {
if (score != null) {
System.out.println("Score: " + score);
} else {
System.out.println("Member not found");
}
});
  1. If the member does not exist, the method returns null
  2. If the key does not exist, the method also returns null
  3. The score is returned as a Double type