rand
英[r?nd] 美[r?nd]
n. The padding between the heel of the shoe and the sole, edge
plural : rands rand
member
UK[?memb?(r)] US[?m?mb?]
n. Member; molecule; body part (especially arm or leg); member, part
plural: members
redis SRANDMEMBER command syntax
Function: If only the key parameter is provided when the command is executed, a random element in the collection will be returned.
Syntax: SRANDMEMBER key [count]
Description: Starting from Redis version 2.6, the SRANDMEMBER command accepts the optional count parameter: if count is a positive number and less than the cardinality of the collection, then the command returns an array containing count elements, and the elements in the array are different. If count is greater than or equal to the collection cardinality, then the entire collection is returned. If count is negative, the command returns an array, the elements of which may appear multiple times, and the length of the array is the absolute value of count . This operation is similar to SPOP, but SPOP removes random elements from the collection and returns it, while SRANDMEMBER only returns random elements without making any changes to the collection.
Available versions: >= 1.0.0
Time complexity: O(1) when only key parameter is provided. If the count parameter is provided, it is O(N), where N is the number of elements in the returned array.
Return: When only the key parameter is provided, an element is returned; if the collection is empty, nil is returned. If the count parameter is provided, an array is returned; if the collection is empty, an empty array is returned.
redis SRANDMEMBER command example
# 添加元素 redis> SADD fruit apple banana cherry (integer) 3 # 只給定 key 參數(shù),返回一個隨機元素 redis> SRANDMEMBER fruit "cherry" redis> SRANDMEMBER fruit "apple" # 給定 3 為 count 參數(shù),返回 3 個隨機元素 # 每個隨機元素都不相同 redis> SRANDMEMBER fruit 3 1) "apple" 2) "banana" 3) "cherry" # 給定 -3 為 count 參數(shù),返回 3 個隨機元素 # 元素可能會重復出現(xiàn)多次 redis> SRANDMEMBER fruit -3 1) "banana" 2) "cherry" 3) "apple" redis> SRANDMEMBER fruit -3 1) "apple" 2) "apple" 3) "cherry" # 如果 count 是整數(shù),且大于等于集合基數(shù),那么返回整個集合 redis> SRANDMEMBER fruit 10 1) "apple" 2) "banana" 3) "cherry" # 如果 count 是負數(shù),且 count 的絕對值大于集合的基數(shù) # 那么返回的數(shù)組的長度為 count 的絕對值 redis> SRANDMEMBER fruit -10 1) "banana" 2) "apple" 3) "banana" 4) "cherry" 5) "apple" 6) "apple" 7) "cherry" 8) "apple" 9) "apple" 10) "banana" # SRANDMEMBER 并不會修改集合內(nèi)容 redis> SMEMBERS fruit 1) "apple" 2) "cherry" 3) "banana" # 集合為空時返回 nil 或者空數(shù)組 redis> SRANDMEMBER not-exists (nil) redis> SRANDMEMBER not-eixsts 10 (empty list or set)