Redis Srandmember command
Redis Srandmember command is used to return a collection of random elements.
Starting Redis 2.6 version, Srandmember command accepts an optional count parameter:
- If the count is positive and less than the base set, the command returns an array contains count elements, each element in the array is not the same. If the count is greater than equal to the base set, then returns the entire collection.
- If the count is negative, then the command returns an array of elements in the array may be repeated several times, and the length of the array is the absolute value of the count.
The operation and SPOP similar, but SPOP will be removed and returned a random element from the collection, and then simply return Srandmember random element, without any changes to the collection.
grammar
redis Srandmember basic command syntax is as follows:
redis 127.0.0.1:6379> SRANDMEMBER KEY [count]
Available versions
> = 1.0.0
return value
Only a collection of key parameters, a return element; if the collection is empty, return nil. If a count argument, then returns an array; if the collection is empty, returns an empty array.
Examples
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SRANDMEMBER myset1 "bar" redis 127.0.0.1:6379> SRANDMEMBER myset1 2 1) "Hello" 2) "world"