MSET
Set one or more key-value pairs simultaneously.
If a given key already exists, MSET will overwrite the old value with the new value. Non-existent keys will be created.
MSET is an atomic operation. The entire MSET operation either executes completely or not at all.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”MSET key value [key value ...]Parameter Description
- key: The key to set
- value: The value to set
Detailed Explanation
Section titled “Detailed Explanation”The MSET command is an atomic operation used to set multiple key-value pairs simultaneously. Compared to calling the SET command multiple times, MSET can reduce the number of network round trips and improve performance.
Redisun Usage
Section titled “Redisun Usage”In redisun, the MSET command is implemented through the MSetCommand class and the mset method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Prepare key-value pair mappingMap<String, String> keyValuePairs = new HashMap<>();keyValuePairs.put("key1", "value1");keyValuePairs.put("key2", "value2");keyValuePairs.put("key3", "value3");
// Set multiple key-value pairsboolean result = redisun.mset(keyValuePairs);System.out.println("MSET operation result: " + result);
// Asynchronous versionCompletableFuture<Boolean> future = redisun.asyncMset(keyValuePairs);future.thenAccept(success -> { System.out.println("Async MSET operation result: " + success);});- MSET is an atomic operation. Either all execute successfully or all fail
- If a key already exists, its value will be overwritten by the new value
- Better performance than calling the SET command multiple times, as it reduces network round trips
- If the number of keys and values do not match, it may cause an error