Redis Getset Command

Redis Strings

The Redis Getset command is used to set the value of the 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. When the key has no old value, that is, when the key does not exist, nil is returned.

When the key exists but is not a string type, an error is returned.

Example

First, set the value of mykey and extract a substring.

redis> GETSET db mongodb    # 没有旧值,返回 nil
(nil)

redis> GET db
"mongodb"

redis> GETSET db redis      # 返回旧值 mongodb
"mongodb"

redis> GET db
"redis"

Redis Strings

Other Extensions