English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis Setnx Command

Redis Strings (string)

Redis Setnx (SET if Not eXThe SET command sets the value of the specified key. If the key does not exist, it is created.

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

Setting successful, return 1 . Setting failed, return 0 .

Online Examples

redis> EXISTS job                # job does not exist
(integer) 0
redis> SETNX job "programmer"    # job set successfully
(integer) 1
redis> SETNX job "code-farmer"   # Attempt to overwrite job, failed
(integer) 0
redis> GET job                   # Not overwritten
"programmer"

Redis Strings (string)