SET

Unordered non-repeating collection

Add or delete check move

The title
sadd Add one or more member elements to the collection key. Members that already exist in the collection are ignored. If the key does not exist, create a collection containing only member elements as members.An error is returned when key is not a collection type.
smembers Returns all members of the collection key. A key that does not exist is considered an empty set.
sismember Determines whether the member element is a member of the set key. Return value: 1 exists, 0 does not exist
scard Returns the cardinality of the set key (the number of elements in the set).
srandmember If only the key argument is supplied when the command executes, a random element of the collection is returned.details
spop Removes and returns a random element from the collection.
smove Move the member element from the source collection to the Destination collection.details
127.0.0.1:6379> sadd myset a b c d e  Create a set
(integer) 5
127.0.0.1:6379> smembers myset       Query the set element
1) "c"
2) "a"
3) "d"
4) "b"
5) "e"127.0.0.1:6379 > sismember myset cCheck whether an element in a set exists
(integer) 1
127.0.0.1:6379> sismember myset z    
(integer) 0                         # 0 does not exist127.0.0.1:6379 > scard myset (integer) 5 127.0.0.1:6379> srem myset CRemove an element
(integer) 1
127.0.0.1:6379> smembers myset
1) "b"
2) "d"
3) "a"
4) "e"127.0.0.1:6379 > srandmember myset# random query
"b"127.0.0.1:6379 > srandmember myset"e"127.0.0.1:6379 > srandmember myset"d"127.0.0.1:6379 > spop myset# random popup
"a"127.0.0.1:6379 > smembers myset 1)"b"
2) "d"
3) "e"
127.0.0.1:6379> sadd myset2  g h i j k   
(integer) 5 127.0.0.1:6379> smove myset myset2dMove an element from one set to another
(integer) 1
127.0.0.1:6379> smembers myset2
1) "i"
2) "h"
3) "d"
4) "j"
5) "g"
6) "k"
Copy the code

Set operations

The title
sdiff Returns all members of a set that is the difference between all given sets. Keys that do not exist are considered empty sets.
sinter Returns all members of a set that is the intersection of all given sets. Keys that do not exist are considered empty sets. When there is an empty set in a given set, the result is also empty (according to the law of set operations).
sunion Returns all members of a set that is the union of all given sets. Keys that do not exist are considered empty sets.
127.0.0.1:6379> sadd set1 a b c d  
(integer) 4
127.0.0.1:6379> sadd set2 c d e
(integer) 3
127.0.0.1:6379> sdiff set1 set2   # difference set. There are elements in front but not behind
1) "a"
2) "b"127.0.0.1:6379 > sinter characters set2# intersection
1) "c"
2) "d"127.0.0.1:6379 > sunion characters set2# and set
1) "d"
2) "b"
3) "c"
4) "a"
5) "e"
Copy the code