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).
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”ZSCORE key memberParameter Description
- key: The key of the sorted set
- member: The name of the member
Detailed Explanation
Section titled “Detailed Explanation”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.
Return Value
Section titled “Return Value”- If the member exists, return its score (in string form)
- If the member does not exist or the key does not exist, return nil
Example
Section titled “Example”redis> ZADD myzset 1 "one"(integer) 1redis> ZSCORE myzset "one""1"redis> ZSCORE myzset "two"(nil)Redisun Usage
Section titled “Redisun Usage”In redisun, the ZSCORE command is implemented through the ZScoreCommand class and the zscore method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setHost("localhost"); options.setPort(6379);});
// Add some test dataredisun.zadd("myzset", 1.0, "one");redisun.zadd("myzset", 2.5, "two");
// Get the score of a memberDouble 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 memberDouble nonExistentScore = redisun.zscore("myzset", "three");System.out.println("Score of 'three': " + nonExistentScore); // Output: Score of 'three': nullAsynchronous Usage
Section titled “Asynchronous Usage”// Asynchronously get the score of a memberCompletableFuture<Double> future = redisun.asyncZscore("myzset", "one");
// Handle asynchronous resultfuture.thenAccept(score -> { if (score != null) { System.out.println("Score: " + score); } else { System.out.println("Member not found"); }});- If the member does not exist, the method returns null
- If the key does not exist, the method also returns null
- The score is returned as a Double type