Save if Not eXists
If the key does not exist in the database, it stores the key and value.
SETNX is short for "SET if Not eXists".
SETNX key value.
Integer reply, specifically:
- 1 if the key was set
- 0 if the key was not set
|command>||setnx key Hello|
|command>||setnx key Redis|
|result>||0 Failed to store the key already exists in the database |
|result>||Hello The first stored value 'Hello' is retrieved. |
||This command is equivalent to the
SET key value NX command.
SETNX key value
- Available since 1.0.0.
- Time complexity: O(1)
To practice the command, click here to run
Redis Web Client