Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Setnx command

Redis Setnx command

Redis string

Redis Setnx (SET if Not eXThe Setnx command sets the specified value for key only 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 Version

= 1.0.0

Return Value

Returns 1 on success. Returns 0 on failure.

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