Introduction to Redis GETSET
Redis getset is used to replace the set with GET argument while writing or migrating the new code. The redis getset function sets a key to a value automatically and returns the previous values that were saved in the key. Getset will return an error if the key exists but does not contain a string value. It discards the previous key that was associated with the set operation.
Key Takeaways
- Redis getset is used to assign the new value to the specified key and also displays the old value in the same command.
- If the key does not exist then it will return the nil value in the output. If the key is present, then it will display the old value.
What is Redis GETSET?
We are using getset with the INCR for counting by using atomic reset. It automatically sets the specified key and string value, which was previously returning the old value. In cases when the key is present but does not contain a string value, GETSET will return an error. Setting the key and value is performed with the GETSET command. We are using the getset key with a new key that we created in a database. We must use the key name parameter with the redis getset command.
How to Use Redis GETSET?
To use the GETSET command we need to install the redis server.
1. First step we are to install the redis server in the ubuntu system. We are installing the same by using the apt command as follows.
Command:
# apt install redis-server
Output:
2. In the second step we are starting the redis server by executing the redis-server command. The below example shows to start the redis server as follows.
Command:
# redis-server
Output:
3. In the third step we are login into the redis cli for executing the GETSET command as follows. To login in the redis cli, we are using the below command as follows.
Command:
# redis-cli
Output:
4. While starting the redis server, now in this step we are executing the getset command. We are using the key name as key_getset and defining the value as “test key” as follows.
Command:
GETSET key_getset "test key"
Output:
5. In the above example we can see that the first time it returns nil in output. However, if we use the same key again, it will retrieve the value that we set the first time. In the below example, we can see that we are using the same key name as key_getset but we are using different values, then it will retrieve the value that we set the first time i.e. “test key” as follows.
Command:
GETSET key_getset "test key example"
Output:
Redis GETSET Command
Basically, the GETSET command is used to get the first value from the key that we have set, as well as to set the new value that we define when we execute the getset command. Redis getset command accepts two parameter names as the name of the key and value of the key which we define.
Syntax:
GETSET key name key value
At the time of using the getset command in redis, we need to use two parameters.
Below is the description of the redis getset parameter:
- GETSET – This command is used in redis to get a new value from the key and display the old value.
- Key name – This is the name of key that we are using with getset command in redis.
- Key value – This parameter is nothing but the value which we define for the specified key.
The below example shows how the GETSET command is working. We are setting the first value by using the set command. Then we are using getset command on the same key and again set the value. After using the getset key we are using the get command to retrieve the value which was set by getset command.
Command:
SET key31 “my key”
GETSET key31 “my key1”
GET key31
Output:
Using the GETSET key we are defining the key name by using the getset command. In the below example, we are defining the key name as key32 as follows.
Command:
GETSET key32 "redis geset key"
GET key32
Output:
Redis GETSET New Key Value
If the key does not exist in the database server, the getset command creates a new key and assigns the value defined with the key. When creating a new key, it will return a nil value in the output. In the example below, we can see that we are creating a new key called key33, but this key does not exist in the DB server, so it is creating and returning a nil value.
Command:
GETSET key33 “val33”
Output:
After executing the getset command, we can now execute the same key with a different value, and it will return the result as the value we set during the previous execution, as shown below.
Command:
GETSET key33 "val34"
Output:
Suppose the key does not exist in the database then the GETSET command creates a new key. In the below example, we are creating a new key with the GETSET command as follows.
Command:
GETSET key61 "val61"
GETSET key61 "val62"
Output:
Redis GETSET INCR Command
We are using the redis getset command with incr to display the value. For counting and atomic number reset, we use the getset command with incr. We can refer to this process as incr against the key. In the following example, we are calling the process against the key name key71 every time an event occurs, but we need to get the value from the counter from time to time and automatically reset the value. We do the same thing by using the getset command with the key name key71 and setting the value to 0.
Below example shows how the incr command works with the GETSET command as follows:
Command:
INCR key71
GETSET key71 “0”
GET key71
Output:
In the below example, we are using integer value while creating the key with the incr command as follows.
Command:
INCR key72
GET key72
GETSET key72 3
GET key72
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of the GETSET command?
Answer: We’re using the GETSET command to display the old value and assign the new value to the key we created with the getset command.
Q2. What is the use of the incr command in the redis getset command?
Answer: The incr command with the getset command is used in the counting of atomic reset. We are using the same key with the incr and getset commands.
Q3. Which parameter do we need to pass at the time of using the redis getset command?
Answer: We need to pass two parameters at the time of using the GETSET command i.e. key name and key value.
Conclusion
We are using getset with the incr for counting by using atomic reset. The Redis getset command sets the specified redis key and string value, which was previously returning the old value. When the key exists but the value of the string is not present, Getset will return an error. In it, the previous key associated with the set operation is discarded.
Recommended Articles
This is a guide to Redis GETSET. Here we discuss the introduction, use, redis GETSET command, new key value, and INCR command. You can also look at the following articles to learn more –