top of page

Redis Sets

Redis sets are collections of unique strings that are not sorted. They are implemented using a hash table, which allows for extremely quick membership checks and element additions/removals. Redis sets are an advanced data structure that can be used for many things, including tracking unique items, representing relations, and performing set operations.

1. Size Limitation

A Redis set can have a maximum of 4,294,967,295 members. This is a very huge number, and you are unlikely to ever need to keep a set of this size.

2. Create a Set

In Redis, the SADD command is used to add one or more members to a set. If a member of the set already exists, it is ignored. If the key does not already exist, a new set is formed before the requested members are added.> SADD testset empcode1 empcode2
(integer) 2>

3. Get all the members in a set In Redis, the SMEMBERS command returns all members of a set. If the key does not exist, the function returns a new empty set.> SMEMBERS testset
1) "empcode1"
2) "empcode2">

4. Get the number of members in a set

In Redis, the SCARD command is used to return the number of members in a set.> SCARD testset
(integer) 2>

So, in the previous result, we can see that there are two members in a set called testset.

5. Check if a given value is a member of a set

The SISMEMBER command in Redis is used to check whether a member exists in a set. If the key does not exist, a value of 0 is returned.> SISMEMBER testset empcode2
(integer) 1>

In the above example, we tried to find the member "empcode2" in a set. We got a value of 1, indicating that the member "empcode2" exists in the set "testset."

6. Remove one or more members from a set

In Redis, the SREM command is used to delete one or more members from a set. The member can be ignored if it does not exist in the set. If the key does not exist, the set is considered empty, and this operation returns 0.> SREM testset empcode1
(integer) 1> SMEMBERS testset
1) "empcode2">

We removed the element "empcode1" from the set "testset" in the above example.


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page