Common usage of Redis collection (Set)

Redis's Set is an unordered collection of string type. Collection members are unique, which means that duplicate data cannot appear in the collection.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).
The maximum number of members in a collection is 2^32 - 1 (4294967295, each collection can store more than 4 billion members).


1 SADD key member1 [member2]
Add one or more members to the set (note: the value is unique)







2, the traversal instruction of the set set, smember set





3 SCARD key
to obtain the number of members of the set (equivalent to obtaining the length of the set set)







4 SDIFF key1 [key2]
returns the difference set of all the given sets (if there is only one set, then the smembers instruction is the same as traversing all), if there are two sets, then the comparison result is the result after deduplication: (removed is the first One set is the standard, remove the value contained in the second set, and return all the values ​​after deduplication)









5 SDIFFSTORE destination key1 [key2]
Returns the difference set of all the given sets and stores them in the destination (if there is only one set, then Similar to copying a set set to the target set, please see the remarks in 4 for the value of the difference set compared)







6 SINTER key1 [key2]
Returns the intersection of all the given sets (another method that can traverse the set)






7 SINTERSTORE destination key1 [key2]
Returns the intersection of all the sets given and stores it in the destination (if a set, then it is a backup number method)







8 SISMEMBER key member
Determines whether the member element is a member of the set key (returns 1 if it exists, returns 0 if it does not exist)






9 SMEMBERS key
returns all members in the set (as above)




10 SMOVE source destination member
moves the member element from the source set to the destination set (smove set3 set8 value)
Note that the resource set is set3, moved to set8, and the value of the move is value 
(Note: only one value can be removed)






11 SPOP key
is removed and the random value is returned







12 SRANDMEMBER key [count]
Returns one or more random numbers in the set (if count is not filled in, the default is one, if count is greater than the set Count of all elements, then return all counts)





13 SREM key member1 [member2]
removes one or more members from the set (if value does not exist, then returns 0)





13 SUNION key1 [key2]
returns all the given sets The union of (if it is a set, it is equal to traversing the set)







14 SUNIONSTORE destination key1 [key2]
The union of all the given sets is stored in the destination set and merged, removing duplicate values






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326405599&siteId=291194637