SETNX

Redis Developer Course Redis Cloud Service Redis-Gate Server

Save if Not eXists

If the key does not exist in the database, it stores the key and value.   SETNX is short for "SET if Not eXists".
Usage is SETNX key value.

Return value

Integer reply, specifically:

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

Example

command>setnx key Hello
result>1
command>setnx key Redis
result>0   Failed to store the key already exists in the database  
command>get key
result>Hello   The first stored value 'Hello' is retrieved.  

Animation

Note This command is equivalent to the SET key value NX command.


Syntax

SETNX key value

  • Available since 1.0.0.
  • Time complexity: O(1)
Related Commands SET, MSETNX
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< INCRBYFLOAT SETNX SETEX >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.