Set key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for "SET if Not eXists".

Return value

Integer reply, specifically:

1 if the key was set 0 if the key was not set


redis> SETNX mykey "Hello"

(integer) 1

redis> SETNX mykey "World"

(integer) 0

redis> GET mykey

"Hello" redis>