?
This document uses PHP Chinese website manual Release
SPOP key [count]
自1.0.0起可用。
時間復(fù)雜度: O(1)
從設(shè)置值存儲中移除并返回一個或多個隨機元素key
。
該操作與 SRANDMEMBER 相似,它返回一個或多個隨機元素,但不會將其刪除。
該count
參數(shù)自 3.2 版開始可用。
批量字符串回復(fù):已移除的元素或nil
何時key
不存在。
redis> SADD myset "one" (integer) 1
redis> SADD myset "two" (integer) 1
redis> SADD myset "three" (integer) 1
redis> SPOP myset "one"
redis> SMEMBERS myset 1) "two" 2) "three"
redis> SADD myset "four" (integer) 1
redis> SADD myset "five" (integer) 1
redis> SPOP myset 3 1) "four" 2) "three" 3) "five"
redis> SMEMBERS myset 1) "two"
如果 count 大于 Set 內(nèi)部的元素數(shù)量,則該命令將只返回整個集合而不包含其他元素。
請注意,當您需要保證返回元素的均勻分布時,此命令不適用。有關(guān)用于 SPOP 的算法的更多信息,請查找 Knuth 采樣和 Floyd 采樣算法。
Redis 3.2引入了一個可選count
參數(shù),可以傳遞給 SPOP 以便在一次調(diào)用中檢索多個元素。