Strings Setnx
# Redis SETNX Command
[!(#) Redis Strings](#)
The Redis SETNX (**SET** if **N**ot e**X**ists) command sets the value for a specified key only if the key does not already exist.
### Syntax
The basic syntax for the Redis SETNX command is as follows:
redis 127.0.0.1:6379> SETNX KEY_NAME VALUE
### Available Since
>= 1.0.0
### Return Value
Returns 1 if the setting is successful.
Returns 0 if the setting fails.
### Example
redis> EXISTS job # job does not exist
(integer) 0
redis> SETNX job "programmer" # job is set successfully
(integer) 1
redis> SETNX job "code-farmer" # attempt to overwrite job, fails
(integer) 0
redis> GET job # was not overwritten
"programmer"
[!(#) Redis Strings](#)
AI is thinking...
[](#)(#)
(#)[](#)
YouTip