Redis supports several data types for storing collections of items. These include lists, sets, and hashes. Last week's blog article covered the List data type and highlighted some of the main commands for managing them. In today's follow-up we'll be turning our attention to the set type. In Redis, a Set is similar to a List except that it doesn't keep any specific order for its elements and each element must be unique. This article will go over some of the main commands for managing sets, both via the redis-cli and using Navicat for Redis.
Creating a Set
In Redis, we can create a Set by using the SADD command that adds the specified members to the key:
SADD key member [member ...]
As mentioned previously, each element must be unique. For that reason, specified members that are already part of the Set are ignored. If the key doesn't exist, a new Set is created and the unique specified members are added. If the key already exists or it is not a Set, an error is returned.
Here's the command to create a "vehicles" set:
SADD vehicles "Infiniti" // 1 SADD vehicles "Mazda" // 1 SADD vehicles "Ford" "Mercedes" // 2 SADD vehicles "Porsche" "Mercedes" // 1
Note that the SADD command returns the number of members that were added in that statement, not the size of the Set. We can see in the last line that only one element was added as there was already a "Mercedes" value.
Creating a Set in Navicat
In the Navicat for Redis Editor, Set values are represented as a Elements. Clicking on the ellipsis [...] button on the right of the Element opens a special Editor where you can enter individual Set elements:
Clicking the Apply button adds the new Set or element. Navicat automatically removes duplicate values.
Removing Members From a Set
We can remove members from a Set by using the SREM command:
SREM key member [member ...]
SREM vehicles "Mazda" "Mercedes" // 2 SREM vehicles "Dodge" // 0
Similar to the SADD command, SREM return the number of members that were removed.
In the Navicat Editor, we can remove any Set element by selecting it and clicking the Delete [-] button located under the Element values:
Verifying That a Value Exists
To verify that a member is part of a Set, we can use the SISMEMBER command:
SISMEMBER key member
If the member is part of the Set, this command returns 1; otherwise, it returns 0:
SISMEMBER vehicles "Infiniti" // 1 SISMEMBER vehicles "Alfa Romeo" // 0
Viewing a Set
To show all the members that exist in a Set, we can use the SMEMBERS command:
SMEMBERS key
Let's see what is currently contained in the vehicles Set:
SMEMBERS vehicles // returns "Infiniti", "Ford", "Porsche"
Merging Sets
We can combine Sets very easily using the SUNION command:
SUNION key [key ...]
Each argument to SUNION represents a Set that we want to merge into a larger Set. Note that any overlapping members will be removed in order to maintain element uniqueness.
Say that we had another Set named more_vehicles that contained the values "Corvette" and "Alfa Romeo". We could view all the members of both the vehicles and more_vehicles Sets as follows:
SUNION vehicles more_vehicles // "Infiniti", "Ford", "Porsche", "Corvette", "Alfa Romeo"
Conclusion
This blog article highlighted some of the main commands for managing Sets in Redis, both via the redis-cli and using Navicat for Redis.
Interested in giving Navicat for Redis a try. Download it here. The trial version is fully functional for 14 days.