getset.3valkey - Man Page
Returns the previous string value of a key after setting it to a new value.
Synopsis
GETSET
key value
Description
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.
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"
:
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"
Reply
Resp2
One of the following:
- valkey-protocol(7) Bulk string reply: the old value stored at the key.
- valkey-protocol(7) Nil reply: if the key does not exist.
Resp3
One of the following:
- valkey-protocol(7) Bulk string reply: the old value stored at the key.
- valkey-protocol(7) Null reply: if the key does not exist.
Complexity
O(1)
Acl Categories
@fast @string @write
History
- Available since: 1.0.0
Notes
This command is deprecated (since 6.2.0) and replaced by SET
with the GET
argument.
Examples
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"
See Also
append(3valkey), decr(3valkey), decrby(3valkey), get(3valkey), getdel(3valkey), getex(3valkey), getrange(3valkey), incr(3valkey), incrby(3valkey), incrbyfloat(3valkey), lcs(3valkey), mget(3valkey), mset(3valkey), msetnx(3valkey), set(3valkey), setrange(3valkey), strlen(3valkey)