Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Msetnx command

Redis Msetnx command

Redis string

The Redis Msetnx command is used to set one or more key-value pairs at the same time only when all given keys do not exist.

Syntax

The basic syntax of the redis Msetnx command is as follows:



redis 127.0.0.1:6379> MSETNX key1 value1 key2 value2 .. keyN valueN 

Available Version

= 1.0.1

Return Value

Returns 1 when all keys are set successfully. If all given keys fail to be set (at least one key already exists), returns 0.

Example



# MSETNX for non-existent key



redis> MSETNX rmdbs "MySQL" nosql "MongoDB" key-value-store "redis"

(integer) 1



redis> MGET rmdbs nosql key-value-store

1) "MySQL"

2) "MongoDB"

3) "redis"





# Given key in MSET already exists



redis> MSETNX rmdbs "Sqlite" language "python"  # rmdbs 键已经存在,操作失败

(integer) 0



redis> EXISTS language                          # 因为 MSET 是原子性操作,language 没有被设置

(integer) 0



redis> GET rmdbs                                # rmdbs 也没有被修改

"MySQL"

Redis string