Redis GETSET – How to set new and get old string value of a key in redis

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a new and return a old string value of a key. For this, we will use a Redis GETSET command.

GETSET Command

This command set a new string value at a specified key and returns its old string value. It returns NIL, if the key does not exist and returns an error, if a key exist but value stored at the key is not a string. The syntax of Redis GETSET command is as follows:-

Syntax :-

redis host:post> GETSET <keyname> <value>

Output :- 

(string) reply, representing string value stored at key.
(nil) reply, if key does not exist.
Error, if key exist and value stored at the key is not a string.

Example :-

Redis GETSET

References :-

  1. GETSET Command Docs

That’s all for how to set a new string value at a key and returns its old string value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- String Commands