HMSET
Set multiple field-value pairs in the hash table simultaneously.
If the hash table does not exist, HMSET will first create an empty hash table and then perform the operation.
If the field already exists, the old value will be overwritten by the new value.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”HMSET key field value [field value ...]Parameter Description
- key: The key of the hash table
- field: The field in the hash table
- value: The value to set
Detailed Explanation
Section titled “Detailed Explanation”The HMSET command is used to set multiple field-value pairs in the hash table simultaneously. It is the batch version of the HSET command, which can set multiple fields at once, improving write efficiency.
Redisun Usage
Section titled “Redisun Usage”In redisun, the HMSET command is implemented through the HmSetCommand class and the hmset 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 multiple field-value pairsMap<String, String> userHash = new HashMap<>();userHash.put("name", "Alice");userHash.put("email", "alice@example.com");userHash.put("age", "25");userHash.put("city", "New York");
// Set multiple hash table fields simultaneouslyboolean result = redisun.hmset("user:1000", userHash);System.out.println("HMSET result: " + result); // Output: true
// Verify successful settingSystem.out.println("Name: " + redisun.hget("user:1000", "name")); // Output: AliceSystem.out.println("Email: " + redisun.hget("user:1000", "email")); // Output: alice@example.com
// Update some fieldsMap<String, String> updateHash = new HashMap<>();updateHash.put("age", "26");updateHash.put("country", "USA");result = redisun.hmset("user:1000", updateHash);System.out.println("Update result: " + result); // Output: true
// Verify update resultsSystem.out.println("Age: " + redisun.hget("user:1000", "age")); // Output: 26 (updated)System.out.println("Country: " + redisun.hget("user:1000", "country")); // Output: USASystem.out.println("City: " + redisun.hget("user:1000", "city")); // Output: New York (unchanged)
// Asynchronous versionMap<String, String> asyncHash = new HashMap<>();asyncHash.put("phone", "1234567890");asyncHash.put("website", "https://example.com");CompletableFuture<Boolean> future = redisun.asyncHmset("user:1001", asyncHash);future.thenAccept(res -> System.out.println("Async HMSET result: " + res));- If the hash table does not exist, a new hash table will be automatically created
- If the field already exists, the old value will be overwritten by the new value
- If setting only one field-value pair, it is recommended to use the HSET command
- The time complexity of the HMSET command is O(N), where N is the number of fields being set
- Compared to calling HSET multiple times, HMSET can reduce the number of network round trips and improve performance
- Suitable for batch setting multiple properties of an object
- All field-value pairs are either all set successfully or all fail (atomic operation)