Cache field lookup
Read one attribute from a cached object stored as a Redis hash.
Redis · Module
Returns the value of one field in a Redis hash. If the field or its hash key does not exist, Redis returns nil/null.
HGET looks up one field in the hash stored at the specified key. Redis returns that field's string value when present; otherwise it returns a nil or null response when the field or key is absent.
HGET key field| Name | Type | Description |
|---|---|---|
key | key | The name of the key that holds the hash. |
field | field | The field whose value to retrieve. |
A bulk string containing the field value, or a nil/null reply if the field is absent or the key does not exist.
HSET user:42 name "Ada"
HGET user:42 name
HGET user:42 emailStores a hash field, retrieves an existing value, then demonstrates the nil result for a missing field.
Read one attribute from a cached object stored as a Redis hash.
Fetch a single counter, status, or metadata field without reading the complete hash.
Check whether a hash field exists by distinguishing a value response from nil/null.
HGET reads one field from a Redis hash using the key and field arguments. Its average time complexity is O(1), and it returns a bulk string when the field exists or nil when it does not.
Follow-up: How would you read several fields? Use HMGET, which returns an array of values in the same order as the requested fields.
The command syntax is HGET key field. The key identifies the hash, and field identifies the single hash field whose value should be retrieved.
Follow-up: What happens if the key contains a non-hash value? Redis reports a WRONGTYPE error rather than treating the value as a hash.
HGET has O(1) complexity because it retrieves one hash field. It is therefore suitable for targeted reads when an application needs a single attribute rather than the whole hash.
Follow-up: What command retrieves every field and value? HGETALL does so, but its cost grows with the size of the hash.
HSETHMGETHGETALL