HGET
Returns the value of the specified field in the hash table.
If the field does not exist, returns nil.
If the key does not exist, returns nil.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”HGET key fieldParameter Description
- key: The key of the hash table
- field: The field to get the value
Detailed Explanation
Section titled “Detailed Explanation”The HGET command is used to get the value of the specified field stored in the hash table. It is a basic read operation used in conjunction with the HSET command.
Redisun Usage
Section titled “Redisun Usage”In redisun, the HGET command is implemented through the HGetCommand class and the hget 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 fieldsredisun.hset("user:1000", "name", "Alice");redisun.hset("user:1000", "email", "alice@example.com");
// Get hash table field valueString name = redisun.hget("user:1000", "name");System.out.println("User name: " + name); // Output: Alice
// Get non-existent fieldString age = redisun.hget("user:1000", "age");System.out.println("User age: " + age); // Output: null
// Get field of non-existent keyString value = redisun.hget("nonexistent", "field");System.out.println("Value: " + value); // Output: null
// Asynchronous versionCompletableFuture<String> future = redisun.asyncHget("user:1000", "email");future.thenAccept(email -> System.out.println("User email: " + email));- If the given field exists in the hash table, return the value of that field
- If the given field does not exist or the given key does not exist, return null
- The time complexity of the HGET command is O(1)
- Hash tables are suitable for storing objects, and HGET is the basic way to access object properties