Redis Sets: Basic and advance

Redis Sets are an unordered collection of unique strings. Unique strings means there is now not a single string repeated in the set.

In Redis set add, remove, and take a look at for the existence of members in O(1) (constant time regardless of the quantity of factors contained inside the Set). The maximum size of a list is more than 4 billion of factors per set.

EXAMPLE

redis 127.0.0.1:6379> SADD javatpoint db2  
(integer) 1  
redis 127.0.0.1:6379> SADD javatpoint mongodb  
(integer) 1  
redis 127.0.0.1:6379> SADD javatpoint db2  
(integer) 0  
redis 127.0.0.1:6379> SADD javatpoint cassandra  
(integer) 1  
redis 127.0.0.1:6379> SMEMBERS javatpoint  
1) "cassandra"  
2) "db2"  
3) "mongodb"  

In the above example, you can see that we have introduced 4 factors in the set through the usage of SADD command. But solely 3 factors are retrieved through using SMEMBERS command because one issue was duplticate and Redis sets study duplicate values only once.

Redis Sets Commands

Index Command Description
1 SADD key member1 [member2] It is used to add one or more members to a set.
2 SCARD key It is used to getsthe number of members in a set.
3 SDIFF key1 [key2] It is used to subtract multiple sets.
4 SDIFFstore destination key1 [key2] It is used to subtract multiple sets and stores the resulting set in a key.
5 SINTER key1 [key2] It is used to intersect multiple sets.
6 SINTERSTORE destination key1 [key2] It is used to intersect multiple sets and stores the resulting set in a key.
7 SISMEMBER key member It is used to determine if a given value is a member of a set.
8 SMOVE source destination member It is used to move a member from one set to another.
9 SPOP key It is used to remove and returns a random member from a set.
10 SRANDMEMBER key [count] It is used to get one or multiple random members from a set.
11 SREM key member1 [member2] It is used to remove one or more members from a set.
12 SUNION key1 [key2] It is used to add multiple sets.
13 SUNIONSTORE destination key1 [key2] It is used to add multiple sets and stores the resulting set in a key.
14 SSCAN key cursor [match pattern] [count count] It is used to incrementally iterates set elements.

Next TopicRedis Sorted Sets