Skip to main content

SETNX

Syntax

SETNX key value

Time complexity: O(1)

Set key to hold string value if key does not exist. When key already holds a value, no operation is performed. SETNX is short for "SET if Not eXists".

Return

Integer reply, specifically:

  • 1 if the key was set
  • 0 if the key was not set

Examples

dragonfly> SETNX mykey "Hello"
(integer) 1
dragonfly> SETNX mykey "World"
(integer) 0
dragonfly> GET mykey
"Hello"