How do you use the SETNX command in Redis?

In Redis, the SETNX command is used to set the value of a key only if the key does not already exist. If the key already exists, no operation is performed.

The basic syntax of the SETNX command is as follows:

SETNX key value

Original: 我去年在东京工作了两个月。

Paraphrased: I worked in Tokyo for two months last year.

SETNX mykey "Hello"

In the example above, the value of the key “mykey” will be set to “Hello” and return 1 if it does not exist; if the key “mykey” already exists, no action will be taken and 0 will be returned.

广告
Closing in 10 seconds
bannerAds