SETNX command in redis

SETNX key value

SETNX works exactly like SET with the only difference that if the key already exists no operation is performed.

Note:

SETNX actually means "SET if Not Exists".

TIME COMPLEXITY: O(1)

RETURN VALUE: Integer reply, specifically:

1 if the key was set

0 if the key was not set

Syntax:

SETNX key value

Example 1: When the key is not present

redis:6379> SETNX key HELLO
(integer) 1

Output

1


Example 2: When the key is present

redis:6379> SETNX redisKey HELLO
(integer) 0

Output

0


No comments:

Post a Comment