GETSET key value

将键 key 的值设为 value , 并返回键 key 在被设置之前的旧值。

返回给定键 key 的旧值。

如果键 key 没有旧值, 也即是说, 键 key 在被设置之前并不存在, 那么命令返回 nil

当键 key 存在但不是字符串类型时, 命令返回一个错误。

*Design pattern

GETSET can be used together with INCR for counting with atomic reset. For example: a process may call INCR against the key mycounter every time some event occurs, but from time to time we need to get the value of the counter and reset it to zero atomically. This can be done using GETSET mycounter "0":

redis>  INCR mycounter
(integer) 1
redis>  GETSET mycounter "0"
"1"
redis>  GET mycounter
"0"
redis> 

As per Redis 6.2, GETSET is considered deprecated. Please use SET with GET parameter in new code.

*返回值

多行字符串: the old value stored at key, or nil when key did not exist.

*例子

redis>  SET mykey "Hello"
"OK"
redis>  GETSET mykey "World"
"Hello"
redis>  GET mykey
"World"
redis>