Introduction to Redis EXISTS
Redis exists command is used to check whether the specified key exists or not in the redis database server. In redis from version 3.0, we have the possibility to specify multiple keys instead of using a single key. In the same case, it will return the total number of keys that exist on the database server. To return 1 and 0 for the single key is a special case for usage.
- Redis exists is a very useful command which is used to check whether the key exists or not onto the database server.
- At the time of executing the redis exists command it will return 0 and an integer number value in the output. If the key does not exist then it will return a 0 value.
What is Redis EXISTS?
Exist in Redis is used to check whether or not the specified key exists on the database server. If we write one key multiple times, the exist command counts the key multiple times. The Exist command returns a 0 and an integer value. If the specified key does not exist on the server, the output will be zero. If the specified key is found on the server, the output will be an integer value.
If we enter a number of keys in the exist command, it will return the number of keys that exist on the server. We use the redis exist command to check the existence of one or more keys that were present on the database server. The exist command in Redis is used to determine the number of keys on the database server.
How to Check Redis EXIST in the Key?
The below steps show how we can check that redis exists by using the key as follows:
To use redis exist we need to install redis on our system.
1. While using the redis exists command with the key we need to install the redis in our system. In the below example, we install redis on the ubuntu system.
# apt install redis-server
2. While installing the redis server on the ubuntu server, we need to start the same. The below example shows how we can start the redis server.
3. We need to login to the database server while starting the server to check if the key exists in Redis. The below example shows to login into the database server as follows.
4. While logging in the database server, we are creating the key using the set command as follows.
SET key101 val1 SET key 102 val2
5. In this step, we are using the single key with the redis exist command to check the specified key as follows.
6. Now in this step we are using the two keys with redis exist command to check the specified key as follows.
EXISTS key101 key102
Redis EXISTS Keys
We define the specified key while using the exists key to determine whether or not the key exists on the database server. The following syntax shows redis exist key. We recommend using the key name when using the redis exists command.
In the above syntax exist command is used in redis to check the existence of the key in the database server. At the time of using exists command, we need to use the key name with the redis exists command. The below example shows redis will return a multiple number of keys when we define the same key in our command.
SET key103 val3 SET key104 val4 EXISTS key103 key103 key103
In the above example, we can see that we have used key103 three times, so it will show the output as 3 times the integer value. If suppose we are defining the wrong key value then it will show the output as 0 integer value as follows. In the below example, we have used key110 which did not exist in the database so it will return a 0 value as output.
Redis EXISTS Command
The exists command in Redis allows us to determine whether or not a specified key exists in the database. The following example shows how to use the exists command on the newly created key. We created the key105 by using the set command, and then we used the exists command on the specified key to check its existence. We can see that when we use the key105 key with exists command, it returns the integer 1 value in the output.
SET key105 val1 EXISTS key105
In the example below, we are passing multiple keys in a single exists command. When multiple keys are passed, it will return the value of the key that was present in the key. We created key names key11 and key12, but we used the exists command to define three key names: key11, key12, and key13. Because key13 does not exist in our database, it will return two values in the output.
SET key11 val1 SET key12 val12 EXISTS key11 key12 key13
In the below example, we are using an already created key from the database server. We are using mylist key as follows.
Examples of Redis EXISTS
Below examples shows that redis exists command as follows:
Here we are creating key21 and the same we are using in redis exists.
SET key21 val1 EXISTS key21
In this example we are creating three keys in single exists command as follows. We are creating key22, key23 and key24, and the same we are using in exists commands as follows.
SET key22 val1 SET key23 val1 SET key24 val1 EXISTS key22 key23 key24
In this example, we are using a key that does not exists in the database. We are using the key name key31, so it will show the output as zero as follows.
In this example, we are using three keys but it will show the output as 1 because two keys are not present in DB server.
SET key41 val1 EXISTS key41 key42 key43
Given below are the FAQs mentioned:
Q1. What is the use of redis exists command?
Answer: Redis exists command is used to get the value of existing keys from the database. It retrieves the existing key number.
Q2. Which command we are using with exist command to create key?
Answer: For creating the new key, we use the set command in combination with the exists command. We can use the key created with the exists command.
Q3. Which integer value is returned by the Redis exist command?
Answer: If the key is not present, the Redis exist command returns 0; otherwise, it returns an integer number if the key is present.
While using exist in redis it is used to check whether the specified key exists or not in the database server. If suppose we have written one key multiple times then exist command counts the key multiple times. Redis exists command is used to check whether the specified key exists or not in the redis database server.
This is a guide to Redis EXISTS. Here we discuss the introduction, how to check redis exists in the key, examples, and FAQs. You may also have a look at the following articles to learn more –