EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis GETSET
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis GETSET

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.

Redis GETSET

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Install Redis GETSET

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:

Redis GETSET Server

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:

Executing Redis GETSET command

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:

Redis GETSET - Defining the value

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:

Retrieve Redis GETSET value

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:

Setting the First value

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:

key name as key32

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:

Redis GETSET - NIL value

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:

execute the same key with a different value

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:

Create a new key

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:

how incr command works

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:

using integer value

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 –

  1. Redis TLS
  2. Redis RDB
  3. Redis INFO
  4. Redis ZRANGE
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more