Easy Tutorial
❮ Sets Srem Redis Stream ❯

Redis Setnx Command

Redis Strings

The Redis Setnx (SET if N ot eXists) command sets the specified value to a key if the key does not exist.

Syntax

The basic syntax for the redis Setnx command is as follows:

redis 127.0.0.1:6379> SETNX KEY_NAME VALUE

Available Versions

= 1.0.0

Return Value

Returns 1 if the key is set successfully. Returns 0 if the key is not set.

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                   # not overwritten
"programmer"

Redis Strings

❮ Sets Srem Redis Stream ❯