1. sadd key value…. add

  • Adds one or more specified member elements to the collection key.

Member of one or more specified elements

  • Ignore it if it already exists in the collection key.
  • If the collection key does not exist, create a new collection key and add a member element to the collection key.
  • An error is returned if the key type is not a collection.

Returns the number of elements successfully added to the collection, excluding those already in the collection.

2.4: Accept multiple member parameters. Previous versions of Redis 2.4 could only add one member element at a time.

127.0. 01.:6379> sadd set key1 key2 key3
(integer) 3
127.0. 01.:6379> sadd set key1
(integer) 0
127.0. 01.:6379> smembers set
1) "key3"
2) "key1"
3) "key2"
127.0. 01.:6379> 

Copy the code

2. Smembers returns all elements of the key set

3. SISMEMBER key member Checks whether the member exists

Returns whether member is a member of the stored collection key.

  • Returns 1 if the member element is a member of the collection key
  • If the member element is not a member of key, or the collection key does not exist, 0 is returned
127.0. 01.:6379> smembers set
1) "key3"
2) "key1"
3) "key2"
127.0. 01.:6379> sismember set key4
(integer) 0
127.0. 01.:6379> sismember set key3
(integer) 1
Copy the code

Scard key returns the total number of elements

Returns the cardinality (number of collection elements) of the key stored in the collection.

Integer-reply: The cardinality (number of elements) of the set, or 0 if the key does not exist.

127.0. 01.:6379> scard set
(integer) 3
Copy the code

5. Srem key member Removes the specified element

Removes the specified element from the key collection. If the specified element is not a member of the key set, it is ignored. If the key set does not exist, it is considered an empty set. This command returns 0.

If the type of key is not a collection, an error is returned.

Integer-reply: The number of elements removed from the collection, excluding non-existent members.

History := 2.4: accepts multiple member element arguments. Prior to Redis 2.4, you could only remove one element at a time.

127.0. 01.:6379> srem set key4
(integer) 0
127.0. 01.:6379> srem set key3
(integer) 1
127.0. 01.:6379> scard set
(integer) 2
127.0. 01.:6379> smembers set
1) "key1"
2) "key2"
Copy the code

6. Spop key [count] is randomly deleted

Removes and returns one or more random elements from the collection stored in key

The count parameter will be available in later versions, but is not available in 2.6, 2.8, and 3.0

If count is greater than the number of elements in the collection, this command returns the entire collection, with no additional elements.

127.0. 01.:6379> spop set
"key2"
127.0. 01.:6379> spop set
"key3"
127.0. 01.:6379> spop set 3
1) "key1"
Copy the code

7. Srandmember Key [count] Returns elements randomly

Similar to SPOp but without removing Redis 2.6, the count argument can be accepted.

  • If count is an integer and less than the number of elements, return an array containing count distinct elements.
  • If count is an integer greater than the number of elements in the collection, only all elements of the entire collection are returned
  • When count is negative, an array of elements containing the absolute value of count is returned,
  • If the absolute value of count is greater than the number of elements, the result set will return multiple occurrences of an element.

When the value of the count argument is negative, the extraction is like putting the fetched elements back into the package after each extraction, so duplicate elements may be returned, as well as elements that always return the number we requested

127.0. 01.:6379> smembers set
1) "key3"
2) "key1"
3) "key2"
127.0. 01.:6379> srandmember set
"key2"
127.0. 01.:6379> srandmember set
"key1"
127.0. 01.:6379> srandmember set
"key3"
127.0. 01.:6379> srandmember set 2
1) "key1"
2) "key3"
127.0. 01.:6379> srandmember set 2
1) "key3"
2) "key2"
Copy the code

SMOVE source destination member Moves elements to another collection

Move member from the source collection to the Destination collection

  • If the source collection does not exist or contains the specified element, the smove command does nothing and returns 0. Otherwise the object will be removed from the source collection and added to the Destination collection
  • If the element already exists in the Destination collection, the smove command simply removes it from the source collection.
  • If source and Destination are not collection types, an error is returned.
127.0. 01.:6379> smove set list key1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0. 01.:6379> sadd set key1 key2 key3
(integer) 3
127.0. 01.:6379> sadd set2 key3
(integer) 1
127.0. 01.:6379> smove set set2 key3
(integer) 1
127.0. 01.:6379> smembers set
1) "key1"
2) "key2"
127.0. 01.:6379> smembers set2
1) "key3"
Copy the code

9. Union, difference, and intersection

  • SUNION key [key …] Returns all members of the union of a given collection.
  • SDIFF key [key …] Returns the element of the set of differences between a set and the given set.
  • SINTER key [key …] Returns the intersection of specified members of all collections.
  • SINTERSTORE destination key [key …] Similar to the SINTER command, the results are stored in the Destination collection. If the Destination collection exists, it is overridden.
127.0. 01.:6379> sadd a 1 2 3  4 5 6
(integer) 6
127.0. 01.:6379> sadd b 1 2 3  7 8 9
(integer) 6
127.0. 01.:6379> sunion a b
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "Seven"
8) "8"
9) "9"
127.0. 01.:6379> sdiff a b
1) "4"
2) "5"
3) "6"
127.0. 01.:6379> sinter a b
1) "1"
2) "2"
3) "3"
127.0. 01.:6379> sinterstore c a b
(integer) 3
127.0. 01.:6379> smembers c
1) "1"
2) "2"
3) "3"
127.0. 01.:6379> 
Copy the code