Extract hash values
An application needs every cached attribute value from a hash but does not need the attribute names.
Redis · Module
Returns all values stored in a Redis hash. Use it when field names are unnecessary; the result order is not guaranteed.
HVALS reads the hash identified by key and returns the values of every field as an array. It does not return field names; use HGETALL when both fields and values are needed.
HVALS key| Name | Type | Description |
|---|---|---|
key | Redis key | The name of the key that holds the hash. |
An array containing all values in the hash; an absent key returns an empty array.
HSET myhash field1 "Hello"
HSET myhash field2 "World"
HVALS myhashStores two fields in a hash, then returns their values.
An application needs every cached attribute value from a hash but does not need the attribute names.
A service prepares a list of cached values for processing or display.
A test compares the contents of a hash independent of Redis's unspecified iteration order.
HVALS takes one hash key and returns all values stored in that hash. Its time complexity is O(N), where N is the number of hash fields.
Follow-up: How would you retrieve field names as well? Use HGETALL, which returns both fields and values.
The command is HVALS key. The key must identify a hash; the result is an array of values, and an absent key produces an empty array.
Follow-up: What should be done before calling HVALS if a key might contain another data type? Check or enforce the key's type, because a wrong type causes a Redis error.
HVALS is useful when only the values from a hash are needed, such as collecting cached record attributes without transferring field names. It is a read-only hash command.
Follow-up: When would HGET be preferable? Use HGET when one known field is needed, avoiding a full O(N) scan.
HSETHGETHGETALLHMGETHLEN