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

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a new string value at a specified key and returns its old string value. For this we will use a COMMAND – GETSET in redis-cli. This command returns nil, if key does not exist and returns error, if 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.