The set type is also used to store multiple string elements, but unlike a list, its elements are unordered and unrepeatable, and the elements cannot be obtained through an index. As shown in the figure below, the set user:1:follows contains four elements: "his", "it", "sports", and "music", and a set can store up to (2 to the power of 32 - 1) elements.
1) Intra-collection operations (1) Add element sadd key value [value...] The return result is the number of elements that were successfully added.
(2) Delete the element srem key value [value...] The return result is the number of elements that were successfully deleted.
(3) Get the number of elements scard key
(4) Determine whether the element is in the set of sismember key value
(5) Randomly return a specified number of elements from the set SrandMember Key [count] [count] is an optional parameter, if not written, the default is: 1.
(6) Randomly pop elements from the set spop key spop operation can randomly pop an element from the set.
(7) Get all elements of the set smembers key Get all elements of the collection, and the return result is out of order.
2) Operation between collections (1) Find the intersection of multiple sets sinter key [key...]
(2) Find the union of multiple sets sunion key [key...]
(3) Find the difference set of multiple sets sdiff key [key...]
(4) Save the results of intersection, union and difference set.
sinterstore storeKey key [key...] sunionstore storeKey key [key...] sdiffstore storeKey key [key...]
The operation between sets will be more time-consuming when there are many elements, so redis provides the above three commands (original command + store) to save the results of intersection, union and difference sets between sets to storeKey, for example, save the intersection results between two collections of user:1:follows and user:2:follows to user:1_2:follows.
Resources:https://www.cnblogs.com/pirlo21/p/7120935.html
|