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

SET mykey "Hello" GETSET mykey "World" GET mykey

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":

INCR mycounter GETSET mycounter "0" GET mycounter

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active
Deprecated as of Redis v6.2.0.

Return information

One of the following:

RATE THIS PAGE
Back to top ↑