The HVALS
command in Redis is used to retrieve all the values in a hash stored at a specific key. This is useful in scenarios where you need just the values without the associated fields, especially when working with semi-structured data.
Here's an example of how you might use the HVALS command in Python with the redis-py library.
import redis # create a connection to the Redis server r = redis.Redis(host='localhost', port=6379, db=0) # set a hash value r.hset("myhash", "field1", "value1") r.hset("myhash", "field2", "value2") # get all values of the hash values = r.hvals("myhash") print(values) # Output: [b'value1', b'value2']
In this example, we first establish a connection to the Redis server. We then add two fields ("field1" and "field2") with corresponding values ("value1" and "value2") to the hash stored at key "myhash". The hvals
method is then used to retrieve all the values from the hash, and it returns a list of the values.
HVALS
matches the order of the fields as they were inserted.HVALS
can have significant performance implications on large hash objects as it's a blocking operation.HVALS
on a key that has non-hash data type will return an error. Always ensure that the key holds hash data before using HVALS
.HVALS
. This leads to error or None type return. Always check if key exists before using HVALS
.What are the differences between HGETALL and HVALS?
HGETALL
returns all fields and values of the hash stored at the key, while HVALS
only returns the values.
What happens when HVALS is used on an empty hash or a non-existing key?
It returns an empty list in both cases.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.