Latest web development tutorials

Redis Setnx command

Redis string (string)

Redis Setnx (SET if N ot eX ists) command when the specified key does not exist, set the value for the specified key.

grammar

redis Setnx basic command syntax is as follows:

redis 127.0.0.1:6379> SETNX KEY_NAME VALUE

Available versions

> = 1.0.0

return value

Set successfully, returns 1. Setup fails, 0 is returned.

Examples

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 (string)