Article Directory
Set (set)
Redis Set is an unordered collection of string types.Collection members are unique, which means that no duplicate data can appear in the collection.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and finding 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).
Methods in set
sadd
The Sadd command adds one or more member elements to a collection, and member elements that already exist in the collection are ignored.
If the set key does not exist, create a set that contains only the added elements as members.
An error is returned when the collection key is not a collection type.
Note: Prior to Redis 2.4, SADD only accepted single member values.
SADD KEY_NAME VALUE1..VALUEN
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "hello" (integer) 0 redis 127.0.0.1:6379> SMEMBERS myset 1) "hello" 2) "foo"
smembers
The Smembers command returns all members of the collection.A nonexistent collection key is treated as an empty collection.
SMEMBERS KEY
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset1 1) "World" 2) "Hello"
sismember
The Sismember command determines whether a member element is a member of a collection.
SISMEMBER KEY VALUE
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SISMEMBER myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SISMEMBER myset1 "world" (integer) 0
scard
The Scard command returns the number of elements in the collection.SCARD KEY_NAME
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "hello" (integer) 0 redis 127.0.0.1:6379> SCARD myset (integer) 2
spop
The Spop command removes and returns a random element from a collection.
SPOP KEY
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SPOP myset1 "bar" redis 127.0.0.1:6379> SMEMBERS myset1 1) "Hello" 2) "world"
sunion
The Sunion command returns the union of a given set.A nonexistent collection key is treated as an empty set.
SUNION KEY KEY1..KEYN
127.0.0.1:6379> sadd set1 "java" (integer) 1 127.0.0.1:6379> sadd set1 "python" (integer) 1 127.0.0.1:6379> sadd set1 "js" (integer) 1 127.0.0.1:6379> sadd set2 "js" (integer) 1 127.0.0.1:6379> sadd set2 "jquery" (integer) 1 127.0.0.1:6379> sunion set1 set2 1) "jquery" 2) "java" 3) "python" 4) "js"
srandmenber
The Srandmember command is used to return a random element in a set.SRANDMEMBER KEY [count]
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SRANDMEMBER myset1 "bar" redis 127.0.0.1:6379> SRANDMEMBER myset1 2 1) "Hello" 2) "world"
sinter
The Sinter command returns the intersection of all given sets.A nonexistent collection key is treated as an empty set.When there is an empty set in a given set, the result is also an empty set (according to the law of set operation).
SINTER KEY KEY1..KEYN
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SINTER myset myset2 1) "hello"
srem
The Srem command removes one or more member elements from a collection, and nonexistent member elements are ignored.
When key is not a collection type, an error is returned.
Prior to Redis version 2.4, SREM only accepted single member values.
SREM KEY MEMBER1..MEMBERN
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SREM myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SREM myset1 "foo" (integer) 0 redis 127.0.0.1:6379> SMEMBERS myset1 1) "bar" 2) "world"
smove
The Smove command moves the specified member member element from the source collection to the destination collection.
SMOVE is an atomic operation.
If the source collection does not exist or does not contain the specified member element, the SMOVE command does nothing and returns only 0.Otherwise, the member element is removed from the source collection and added to the destination collection.
When a destination collection already contains a member element, the SMOVE command simply deletes the member element from the source collection.
An error is returned when the source or destination is not a collection type.
SMOVE SOURCE DESTINATION MEMBER
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "foo" (integer) 1 redis 127.0.0.1:6379> SMOVE myset1 myset2 "bar" (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset1 1) "World" 2) "Hello" redis 127.0.0.1:6379> SMEMBERS myset2 1) "foo" 2) "bar"
sdiffstore
The Sdiffstore command stores the differences between a given set in a specified set.If the specified collection key already exists, it will be overwritten.
SDIFFSTORE DESTINATION_KEY KEY1..KEYN
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SDIFFSTORE destset myset myset2 (integer) 2 redis 127.0.0.1:6379> SMEMBERS destset 1) "foo" 2) "bar"
sdiff
The Sdiff command returns the difference between the given sets.A nonexistent collection key is treated as an empty set.
SDIFF FIRST_KEY OTHER_KEY1..OTHER_KEYN
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SDIFF myset myset2 1) "foo" 2) "bar"
sscan
The Sscan command iterates over elements in a collection key.
SSCAN KEY [MATCH pattern] [COUNT count]
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "hi" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> sscan myset1 0 match h* 1) "0" 2) 1) "hello" 2) "h1"
sinterstore
The Sinterstore command stores the intersection between a given set in the specified set.If the specified collection already exists, it will be overwritten.SINTERSTORE DESTINATION_KEY KEY KEY1..KEYN
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SINTERSTORE myset myset1 myset2 (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset 1) "hello"
sunionstore
The Sunionstore command stores the union of a given collection in the specified collection destination.
SUNIONSTORE DESTINATION KEY KEY1..KEYN
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "bar" (integer) 1 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"