Redis - String Setnx Command


Advertisements


Redis SETNX command is used to set some string value in Redis key, if the key does not exist in Redis. Fullform of SETNX is SET if Not eXists.

Return Value

Integer reply 1 or 0

  • 1, if the key is set.
  • 0, if the key is not set.

Syntax

Following is the basic syntax of Redis SETNX command.

redis 127.0.0.1:6379> SETNX KEY_NAME VALUE

Example

redis 127.0.0.1:6379> SETNX mykey redis 
(integer) 1 
redis 127.0.0.1:6379> SETNX mykey mongodb 
(integer) 0 
redis 127.0.0.1:6379> GET mykey 
"redis"

redis_strings.htm

Advertisements