You are reading the article How To Create Redis Sadd And Add Multiple Elements? updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Create Redis Sadd And Add Multiple Elements?Introduction to Redis SADD
Redis sadd command is basically used to add members to set which was stored in the key. If a member already exists, it will be ignored when adding a new member. If the key of a member does not exist, the set will be created and members will be added to it. If the value we have stored for this key is not set, an error is returned.
Start Your Free Software Development Course
Web development, programming languages, Software testing & othersKey Takeaways
Redis sadd is used to add the members to the key which we have stored. The return value of redis sadd is an integer.
The number of integer elements that we are adding to the set, the same elements are not included in the elements which were present in the set.What is Redis SADD?
As we know that redis is implemented by using C and it contains an event-driven model similar to the nodejs. The basic difference between nodejs and redis is that nodejs is used libuv and redis contain its own wrapper. The libae will wrap the epoll or select as per our platform or it will also depend on the compile configuration.
Below is the syntax of redis sadd as follows:
Syntax:SADD name_of_key val1 val2 ….. valN
In the above syntax, the sadd is nothing but the redis sadd which we are using to add the members. Name of the key is defined as the key name which we are using at the time of adding members. Value is nothing but the value which we have defined for the member.How to Create Redis SADD and Add Elements?
The below steps show how we are creating the redis sadd and adding the elements as follows. In the first step, we are starting the redis server.
1. For creating the redis sadd and add elements first we are starting the server by using the following command as follows.
2. After starting the redis server now in this step we are checking the status of the server.
3. After checking the status of the server now in this step we are logging into the redis cli for creating redis sadd and add elements.
4. After logging in redis cli in the below example we are creating it by adding 3 members as follows. We are creating the three members set. We are defining the name of set as redis_set and giving the name of members as mem1, mem2, mem3.
Code:SADD redis_set mem1 mem2 mem3
5. After creating it now in this step we are adding a single element to the already created redis sadd. We are adding mem4 to the already created sadd.
Code:SADD redis_set mem4 SMEMBERS redis_set
6. In the below example we are adding three members in a single command to the already created sadd. We are adding mem5, mem6, and mem7 members to the redis_set sadd.
Code:SADD redis_set mem5 mem6 mem7 SMEMBERS redis_set
Output:How to Use Redis SADD Multiple Elements?
We’re using redis sadd to add multiple elements. We can add multiple elements when creating a new sadd, or we can add new elements after the sadd has been created. In the example below, we can see that we are adding multiple elements when creating a new sadd. In the below example we are creating a new sad name as redis_sadd and adding the elements to the sadd name as sadd1, sadd2, sadd3, and sadd4.
Code:SADD redis_sadd sadd1 sadd2 sadd3 sadd4 SMEMBERS redis_sadd
In the above example, we have added multiple elements at the time of creating the new sadd now in the below example we are adding multiple elements into the already created sadd. In the below example, we are adding elements into the redis_sadd and adding the element’s names as sadd5, sadd6, sadd7, and sadd8.
Code:SADD redis_sadd sadd5 sadd6 sadd7 sadd8 SMEMBERS redis_sadd
In the below example we are adding only two elements into the redis_sadd. We are adding elements named sadd9 and sadd10.
Code:SADD redis_sadd sadd9 sadd10 SMEMBERS redis_sadd Redis SADD Set Command
Redis set is referring to the value collection which was given into the key. Each item in the set is known as a member. So we can say that set is now allowing duplicate values. For creating set in redis we are using the sadd command. In the below example, we are creating the set name as redis_set and defining the member of set as set1, set2 and set3.
Code:SADD redis_set set1 set2 set3
In the redis set command while adding list type set members in the group it will return an error.
Code:LPUSH redis_key "SQL" SADD redis_key DB
Basically, the redis set does not contain duplicate values; if we give it a duplicate value, it will take the first one and ignore the others.
Code:SADD redis_set set4 set5 set5 set6 set4 SMEMBERS redis_set
To get all members from the redis set we are using the following command as follows. We are using the SMEMBERS command.
To get the random members from the set we need to specify the number which set we have required as follows.
Code:SRANDMEMBER redis_set 5
Code:SREM redis_set mem1 SMEMBERS redis_set
Using the SPOP command, we can remove the random member from the set, it will remove the member that we defined.
Code:SPOP redis_set 3 SMEMBERS redis_set
Output:Examples of Redis SADD
Given below are the examples mentioned:Example #1
In the below example, we are creating a redis sadd name as color and we are adding red, black, and white colors into it.
Code:SADD color red black white SMEMBERS color
In the below example, we are adding the pink, blue, and green colors into the sadd name as color.
Code:SADD color pink blue green SMEMBERS color
In the below example, we are adding duplicate elements to the sadd but it will not add it will return zero values.
Code:SADD color red black white SMEMBERS color
Redis sadd command is basically used to add members to set which was stored in the key. If suppose members already exist then it will ignore to add the new member. We are using two parameters while using redis sadd first is name of set and another is the member name.Recommended Articles
This is a guide to Redis SADD. Here we discuss the introduction, how to create redis SADD, and add elements. set command and examples. You may also have a look at the following articles to learn more –
You're reading How To Create Redis Sadd And Add Multiple Elements?
Update the detailed information about How To Create Redis Sadd And Add Multiple Elements? on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!