HSET
Set the value of field field in hash table key to value.
If the hash table does not exist, a new hash table is created and the HSET operation is performed.
If the field field already exists in the hash table, the old value will be overwritten.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”HSET key field valueParameter 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 HSET command is used to set the value of a specified field in a hash table. Redis hash tables are collections of key-value pairs, suitable for storing objects.
Redisun Usage
Section titled “Redisun Usage”In redisun, the HSET command is implemented through the HSetCommand class and the hset method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setAddress("redis://127.0.0.1:6379");});
// Set hash table fieldsint result = redisun.hset("user:1000", "name", "Alice");System.out.println("Set result: " + result); // Output: 1 (new field)
// Update existing fieldresult = redisun.hset("user:1000", "name", "Bob");System.out.println("Update result: " + result); // Output: 0 (update field)
// Set multiple fieldsredisun.hset("user:1000", "email", "bob@example.com");redisun.hset("user:1000", "age", "25");
// Asynchronous versionCompletableFuture<Integer> future = redisun.asyncHset("user:1001", "name", "Charlie");future.thenAccept(res -> System.out.println("Async set result: " + res));- If the field is a new field in the hash table and the value is set successfully, return 1
- If the field already exists in the hash table and the old value has been overwritten by the new value, return 0
- If the key does not exist, a new hash table will be automatically created
- Hash tables are suitable for storing objects, such as user information, product information, etc.