Redis Notes 5: Sets of Data Types

Keywords: Redis Java MySQL MongoDB

Command Description Syntax
 SADD add value SADD key member [member...]
SMEMBERS traversal set SMEMBERS key
 SCARD Gets the number of key members SCARD key
 SISMEMBER Determines whether a member has a SISMEMBER key member
 Spop Random Delete Element Spop key
 SREM deletes the specified member SREM key member [member...]
SRANDMEMBER randomly returns members without deleting the original SRANDMEMBER key
 SMOVE moves members of one collection to another SMOVE source destination member
 SDIFF Finds Set Difference Set SDIFF key [key...]
The difference set in the SDIFFSTORE set is stored in the new set SDIFFSTORE destination key [key...]
SINTER Finds Set Intersection SINTER key [key...]
SINTERSTORE stores the collection intersection to the new set SINTERSTORE destination key [key...]
SUNION SOLVING SUNION SET AND SUNION key [key...]
SUNIONSTORE stores the union of sets into the new set SUNIONSTORE destination key [key...]

1,SADD key member [member …]

If one or more member elements are added to the set key, the member elements that already exist in the set will be ignored. If key does not exist, create a collection that contains only member elements as members. When key is not a collection type, an error is returned.
Return value: Number of new elements added to the collection, excluding those ignored.
Example:

127.0.0.1:6379[15]> SADD bbs "discuz.net"   # Add a single element
(integer) 1
127.0.0.1:6379[15]> SADD bbs "discuz.net"   # Add duplicate elements
(integer) 0
127.0.0.1:6379[15]> SADD bbs "tianya.cn" "groups.google.com"   # Adding multiple elements
(integer) 2
127.0.0.1:6379[15]> SMEMBERS bbs
1) "tianya.cn"
2) "groups.google.com"
3) "discuz.net"

2,SMEMBERS key

Returns all members of the collection key. A key that does not exist is considered an empty collection.
Return value: All members of the collection.
Example:

127.0.0.1:6379[15]> EXISTS not_exists_key
(integer) 0
127.0.0.1:6379[15]> SMEMBERS not_exists_key
(empty list or set)
127.0.0.1:6379[15]> SADD language c++ .net java
(integer) 3
127.0.0.1:6379[15]> SMEMBERS language
1) "java"
2) ".net"
3) "c++"

3,SCARD key

Returns the number of elements in the collection.
Return value: The number of elements in a collection. When key does not exist, return 0.
Example:

127.0.0.1:6379[15]> SADD language c++ .net java
(integer) 3
127.0.0.1:6379> SCARD language
(integer) 3

4,SISMEMBER key member

Determine whether the member element is a member of the set key.
Return value: Returns 1 if the member element is a member of the collection. If the member element is not a member of the collection, or the key does not exist, return 0.
Example:

127.0.0.1:6379[15]> SADD "joe's_movies" "hi,lady" "fast five" "2012"
(integer) 3
127.0.0.1:6379[15]> SMEMBERS "joe's_movies"
1) "fast five"
2) "hi,lady"
3) "2012"
127.0.0.1:6379[15]> SISMEMBER "joe's_movies" 2012
(integer) 1

5,SPOP key

Remove and return a random element in the set. If you only want to get a random element but don't want it to be removed from the collection, you can use the SRANDMEMBER command.
Return value: Removed random element. When the key does not exist or the key is an empty set, return nil.
Example:

127.0.0.1:6379[15]> SADD mydb MySql MongoDB Redis
(integer) 3
127.0.0.1:6379[15]> SPOP mydb
"Redis"
127.0.0.1:6379[15]> SMEMBERS mydb
1) "MySql"
2) "MongoDB"
127.0.0.1:6379[15]> SPOP mydb
"MySql"
127.0.0.1:6379[15]> SMEMBERS mydb
1) "MongoDB"

6,SREM key member [member …]

Remove one or more member elements from the set key, and nonexistent member elements are ignored. When key is not a collection type, an error is returned.
Return value: The number of elements that have been successfully removed, excluding those that have been ignored.
Example:

127.0.0.1:6379[15]> SADD languages c lisp python ruby
(integer) 4
127.0.0.1:6379[15]> SMEMBERS languages
1) "python"
2) "lisp"
3) "c"
4) "ruby"

127.0.0.1:6379[15]> SREM languages ruby                 # Remove a single element
(integer) 1
127.0.0.1:6379[15]> SREM languages non-exists-language  #Remove non-existent elements
(integer) 0
127.0.0.1:6379[15]> SREM languages lisp python c        # Remove multiple elements
(integer) 3
127.0.0.1:6379[15]> SMEMBERS languages
(empty list or set)

7,SRANDMEMBER key [count]

Returns a random element in a collection. If count is positive and less than the cardinality of the set, the command returns an array containing count elements, which are different. If count is greater than or equal to the cardinality of the collection, the entire collection is returned. If count is negative, then the command returns an array, where elements may recur multiple times and the length of the array is the absolute value of count.
Return value: When only the key parameter is provided, an element is returned; if the collection is empty, nil is returned. If the count parameter is provided, an array is returned; if the collection is empty, an empty array is returned.
Example:

127.0.0.1:6379> SADD fruit apple banana cherry
(integer) 3
127.0.0.1:6379> SRANDMEMBER fruit 
"banana"
127.0.0.1:6379> SRANDMEMBER fruit 2
1) "cherry"
2) "apple"
127.0.0.1:6379> SRANDMEMBER fruit 10
1) "banana"
2) "cherry"
3) "apple"
127.0.0.1:6379> SRANDMEMBER fruit 0
(empty list or set)
127.0.0.1:6379> SRANDMEMBER fruit -1
1) "apple"

8,SMOVE source destination member

Move member elements from source set to destination set. If the source collection does not exist or does not contain the specified member element, the SMOVE command does not perform any operations and returns only 0. Otherwise, member elements are removed from the source collection and added to the destination collection. When the destination collection already contains member elements, the SMOVE command simply deletes member elements from the source collection. When source or destination is not a collection type, an error is returned.
Return value: Returns 1 if member element is successfully removed. If the member element is not a member of the source collection and no action is performed on the destination collection, it returns.
Example:

127.0.0.1:6379> SADD myset1 a b c d
(integer) 4
127.0.0.1:6379> SADD myset2 b
(integer) 1
127.0.0.1:6379> SMOVE myset1 myset2 a           #Move elements that do not exist in a, myset2
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
1) "b"
2) "c"
3) "d"
127.0.0.1:6379> SMEMBERS myset2
1) "b"
2) "a"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> SADD myset1 a b c d
(integer) 4
127.0.0.1:6379> SADD myset2 b
(integer) 1
127.0.0.1:6379> SMOVE myset1 myset2 b           #Mobile b, myset2 already has elements
(integer) 1
127.0.0.1:6379> SMEMBERS myset1
1) "c"
2) "d"
3) "a"
127.0.0.1:6379> SMEMBERS myset2
1) "b"

9,SDIFF key [key …]

Returns the set between the given sets. The nonexistent set key is treated as an empty set.
Return value: A list containing the difference set members.
Example:

127.0.0.1:6379[15]> SADD myset1 a b c
(integer) 3
127.0.0.1:6379[15]> SADD myset2 c d e
(integer) 3
127.0.0.1:6379[15]> SDIFF myset1 myset2
1) "c"

10,SDIFFSTORE destination key [key …]

Stores the set between the given sets in the specified set. If the specified collection key already exists, it will be overwritten.
Return value: Number of elements in the result set.
Example:

redis 127.0.0.1:6379> SADD myset1 "hello" "foo" "bar"
(integer) 3
redis 127.0.0.1:6379> SADD myset2 "hello" "world"
(integer) 2
redis 127.0.0.1:6379> SDIFFSTORE destset myset1 myset2
(integer) 2
redis 127.0.0.1:6379> SMEMBERS destset
1) "foo"
2) "bar"

11,SINTER key [key …]

Returns the intersection of all given sets. The nonexistent set key is considered an empty set. When there is an empty set in a given set, the result is also an empty set (according to the set operation law).
Return value: List of intersection members.
Example:

redis 127.0.0.1:6379> SADD myset1 "hello" "foo" "bar"
(integer) 3
redis 127.0.0.1:6379> SADD myset2 "hello" "world"
(integer) 2
redis 127.0.0.1:6379> SINTER myset myset2
1) "hello"

12,SINTERSTORE DESTINATION_KEY KEY KEY1..KEYN

Stores the intersection between a given set in a specified set. If the specified collection already exists, it is overwritten.
Return value: List of intersection members.
Example:

redis 127.0.0.1:6379> SADD myset1 "hello" "foo" "bar"
(integer) 3
redis 127.0.0.1:6379> SADD myset2 "hello" "world"
(integer) 2
redis 127.0.0.1:6379> SINTERSTORE myset myset1 myset2
(integer) 1
redis 127.0.0.1:6379> SMEMBERS myset
1) "hello"

13,SUNION KEY KEY1..KEYN

Returns the union of a given set. The nonexistent set key is considered an empty set.
Return value: and integrates a list of members.
Example:

redis 127.0.0.1:6379> SADD myset1 "hello" "foo" "bar"
(integer) 3
redis 127.0.0.1:6379> SADD myset2 "hello" "world"
(integer) 2
redis 127.0.0.1:6379> SUNION myset1 myset2
1) "bar"
2) "world"
3) "hello"
4) "foo"

14,SUNIONSTORE DESTINATION KEY KEY1..KEYN

Store the union of a given set in the specified destination.
Return value: Number of elements in the result set.
Example:

redis 127.0.0.1:6379> SADD myset1 "hello" "foo" "bar"
(integer) 3
redis 127.0.0.1:6379> SADD myset2 "hello" "world"
(integer) 2
redis 127.0.0.1:6379> SUNIONSTORE myset myset1 myset2
(integer) 1
redis 127.0.0.1:6379> SMEMBERS myset
1) "bar"
2) "world"
3) "hello"
4) "foo"

(Finish!)

Reference resources: http://blog.csdn.net/thinkercode/article/details/46575413

Posted by xSN1PERxEL1TEx on Tue, 09 Apr 2019 21:00:31 -0700