GETSET
Deprecated
As of Redis version 6.2.0, this command is regarded as deprecated.
It can be replaced by SET with the GET argument when migrating or writing new code.
GETSET key value
- Available since:
- Redis Open Source 1.0.0
- Time complexity:
- O(1)
- ACL categories:
-
@write,@string,@fast, - Compatibility:
- Redis Software and Redis Cloud compatibility
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.
Required arguments
key
The name of the key.
value
The new value to set.
Examples
Details
Design pattern
You can use GETSET with INCR to count events and reset the counter atomically.
For example, you can call INCR on the key mycounter each time an event occurs. When you need to read the counter value and reset it to zero atomically, call GETSET.
This can be done using GETSET mycounter "0":
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ✅ Standard |
✅ Standard |
Deprecated as of Redis v6.2.0. |
Return information
One of the following:
- Bulk string reply: the old value stored at the key.
- Nil reply: if the key does not exist.