Easy Tutorial
❮ Server Client Kill Sets Smove ❯

Redis Getset Command

Redis Strings

The Redis Getset command is used to set the value of a specified key and return the old value of the key.

Syntax

The basic syntax of the redis Getset command is as follows:

redis 127.0.0.1:6379> GETSET KEY_NAME VALUE

Available Versions

= 1.0.0

Return Value

Returns the old value of the given key. If the key does not have an old value, i.e., the key does not exist, it returns nil.

If the key exists but is not of string type, it returns an error.

Example

First, set the value of mykey and truncate the string.

redis> GETSET db mongodb    # No old value, returns nil
(nil)

redis> GET db
"mongodb"

redis> GETSET db redis      # Returns old value mongodb
"mongodb"

redis> GET db
"redis"

Redis Strings

❮ Server Client Kill Sets Smove ❯