The problem/use-case that the feature addresses
With Redis 6.2 the SET
command allows the option flags XX
and GET
to be used together (getting the old value, but only writing the new one if the key already exists), but not NX
and GET
.
Peeking and creating a key - but not overwriting it if it exists - would be a handy command, e.g. for atomically setting an expiring "lock" value on a key if it doesn't exist.
Description of the feature
The command SET key value NX GET
should behave as follows:
- if key
exists and holds value oldValue
- don't store value
for key
- reply with oldValue
- if key
doesn't exist
- store value
for key
- reply with (nil)
The client can determine if the value was set by checking the reply for (nil)
(yes) or another value (no).
Alternatives you've considered
Other string commands don't seem to support this case either, so alternatives would be issuing multiple commands or scripting.
Additional information
The limitation is not explicitly mentioned in the command documentation and the "Return value" section is ambiguous about it. Another user has also tripped over it: https://github.com/redis/redis/issues/8838
Comment From: itamarhaber
Makes sense to me.
Comment From: jperezr21
Would be nice to document the return behavior here: https://redis.io/docs/latest/commands/set/#return-information