Redis Sets

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

In Redis set add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the Set). The maximum length of a list is more than 4 billion of elements per set.

EXAMPLE

Redis Sets 1

In the above example, you can see that we have added 4 elements in the set by using SADD command. But only 3 elements are retrieved by using SMEMBERS command because one element was duplticate and Redis sets read duplicate values only once.


Redis Sets Commands

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





Latest Courses