Returns the previous string value of a key after setting it to a new value.
GETSET key value
Atomically sets key to value and returns the old value stored at key. Returns an error when key exists but does not hold a string value. Any previous time to live associated with the key is discarded on successful SET operation.
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":
127.0.0.1:6379> INCR mycounter
(integer) 1
127.0.0.1:6379> GETSET mycounter "0"
"1"
127.0.0.1:6379> GET mycounter
"0"
One of the following:
Bulk string reply: the old value stored at the key.
Nil reply: if the key does not exist.
One of the following:
Bulk string reply: the old value stored at the key.
Null reply: if the key does not exist.
O(1)
@fast @string @write
127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> GETSET mykey "World"
"Hello"
127.0.0.1:6379> GET mykey
"World"
SET with the GET argument.APPEND, DECR, DECRBY, GET, GETDEL, GETEX, GETRANGE, INCR, INCRBY, INCRBYFLOAT, LCS, MGET, MSET, MSETNX, SET, SETRANGE, STRLEN.