ZREM
The ZREM command is used to remove one or more specified members from a sorted set. Non-existent members are ignored.
If the key does not exist, it is treated as an empty sorted set and 0 is returned.
The time complexity of the ZREM command is O(M*log(N)), where N is the number of elements in the sorted set and M is the number of members to be removed.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”ZREM key member [member ...]Parameter Description
- key: The key of the sorted set
- member: One or more members to remove
Detailed Explanation
Section titled “Detailed Explanation”The ZREM command removes specified members from a sorted set. If the specified members do not exist in the sorted set, they are ignored. If the sorted set does not exist, it is treated as an empty sorted set.
Return Value
Section titled “Return Value”The command returns the number of members successfully removed, excluding non-existent members.
Example
Section titled “Example”redis> ZADD myzset 1 "one" 2 "two" 3 "three"(integer) 3redis> ZREM myzset "two"(integer) 1redis> ZRANGE myzset 0 -1 WITHSCORES1) "one"2) "1"3) "three"4) "3"redis> ZREM myzset "four"(integer) 0Redisun Usage
Section titled “Redisun Usage”In redisun, the ZREM command is implemented through the ZRemCommand class and the zrem 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.0, "two");redisun.zadd("myzset", 3.0, "three");
// Remove a single memberlong removedCount = redisun.zrem("myzset", "two");
// Remove multiple memberslong removedCount2 = redisun.zrem("myzset", "one", "three");Asynchronous Usage
Section titled “Asynchronous Usage”// Asynchronously remove membersCompletableFuture<Long> future = redisun.asyncZrem("myzset", "two");
// Handle asynchronous resultfuture.thenAccept(removedCount -> { System.out.println("Removed " + removedCount + " members");});- Non-existent members are ignored and no error is generated
- If the key does not exist, it is treated as an empty sorted set
- The command returns the number of members successfully removed