English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Redis Srandmember command is used to return a random element from the set.
From Redis 2.6 Starting from the version, the Srandmember command accepts an optional count parameter:
This operation is similar to SPOP, but SPOP removes a random element from the set and returns it, while Srandmember only returns a random element without making any changes to the set.
The basic syntax of the redis Srandmember command is as follows:
redis 127.0.0.1:6379> SRANDMEMBER KEY [count]
>= 1.0.0
Only the set key parameter is provided, an element is returned; if the set is empty, nil is returned. If the count parameter is provided, an array is returned; if the set is empty, an empty array is returned.
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"