Redis Msetnx Command
The Redis Msetnx command is used to set one or more key-value pairs only if none of the given keys exist.
Syntax
The basic syntax for the redis Msetnx command is as follows:
redis 127.0.0.1:6379> MSETNX key1 value1 key2 value2 .. keyN valueN
Available Versions
= 1.0.1
Return Value
Returns 1 if all keys are successfully set. Returns 0 if the operation fails for all given keys (at least one key already exists).
Examples
# MSETNX on non-existing keys
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"
# MSETNX with a key that already exists
redis> MSETNX rmdbs "Sqlite" language "python" # rmdbs key already exists, operation fails
(integer) 0
redis> EXISTS language # since MSET is atomic, language is not set
(integer) 0
redis> GET rmdbs # rmdbs is not modified
"MySQL"