-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Description
SET and variants
SET is one of the most used Redis commands, and has many variants:
SETNX-- Set if not existsSETEX-- Set with expirePSETEX-- Set with milliseconds expire
There is demand for additional variants such as SETEXNX, becuase it is a pretty common pattern in caching and sessions handling to set an object only if not already inside the cache, and setting it with an expire. This is also useful for poor's man locking, and so forth.
There is the argument that this need can be addressed with scripting, however probably this is enough common that a very high performance solution is expected, and also we can exploit the need in order to make our SET story more clear for the users.
Trivia: the reason currently we have this mess of commands is because of an early limitation of the Redis protocol in the early stages. The binary-safe argument was only possible as last argument, so it was not easy to extend already existing commands, and I kept introducing new ones. Finally the protocol was fixed and now our hands are free.
Proposal
This proposal extends the SET command to cover all the already existing variants in form of options, so that later all the SET variants will be deprecated.
At the same time the proposal adds a new functionality in form of an option that only executes the SET command if the key already exists.
The new form of the command will be:
SET key value option option option ...
The available options will be:
- nx -- Execute the SET only if the key does not already exists
- xx -- Execute the SET only if the key already exists
- ex -- Set an expire if the SET will be executed.
- px -- Set a milliseconds expire if the SET will be executed.
Examples:
SET foo bar xx # Set foo to bar only if key foo already exists.
SET foo value ex 1000 nx # Set foo to value with an expire of 1000 seconds, if not exists.
SET foo bar ex 10 # Same as SETEX foo 10 bar
The new command will allow more variants in the future if needed and is simple to remember. The option names were chosen to be short to write and easy to remember just remembering the first character:
- Non existing
- eXisting
- Eexpire
- Pprecision expire
Feedbacks welcomed.