Redis Setnx Command

Redis String

Redis Setnx(SET if Not eXThe Redis Setnx command sets the specified value for the key when the specified key does not exist.

Syntax

The basic syntax of 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 set is successful. Returns 0 if the set fails.

Example

redis> EXISTS job                # job 不存在
(integer) 0

redis> SETNX job "programmer"    # job 设置成功
(integer) 1

redis> SETNX job "code-farmer"   # 尝试覆盖 job ,失败
(integer) 0

redis> GET job                   # 没有被覆盖
"programmer"

Redis String

Other Extensions