Redis String: MSETNX key value [key value ...]
Redis MSETNX Command
The Redis MSETNX command is used to set multiple values to multiple keys, only if none of the already exists. If any one from current operation exists in redis then MSETNX does not perform any operation.
Here are some common uses and scenarios for the "MSETNX" command:
- Atomic Key Creation: Safely create multiple key-value pairs if none of the keys already exist.
- Idempotent Operations: Ensure idempotent operations by setting values conditionally.
- Initialization: Initialize default values or configurations if they are not already set.
- Concurrency Control: Coordinate actions across multiple clients without overwriting existing data.
- Transaction Support: Use in transactions to set multiple keys atomically if they do not exist.
Syntax:
Basic syntax of redis MSETNX command is shown below:
redis 127.0.0.1:6379> MSETNX key1 value1 key2 value2 .. keyN valueN
Available since
1.0.1.
Return Value
Integer replies 1 or 0
- 1, if all keys are set in redis
- 0, if no keys are set in redis
Return Value Type
Integer
Example:
redis 127.0.0.1:6379> MSETNX key1 "Hello" key2 "world" (integer) 1 redis 127.0.0.1:6379> MSETNX key2 "worlds" key3 "third key" (integer) 0 redis 127.0.0.1:6379> MGET key1 key2 key3 1) "Hello" 2) "world" 3) (nil)
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/redis/redis-msetnx-key-value.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics