Hashes in Redis are a key-value data structure that stores a mapping of field names to values. Hashes are similar to Python dictionaries. For storing complex objects or rows in a relational database table, Redis hashes are an excellent choice. Hashes are also useful for counters, caching, and session management.
1. Size Limitation
The Redis hash limit is the maximum number of field-value pairs that can be stored in a Redis hash. This is equal to 4,294,967,295. In reality, your hashes are only limited by the amount of memory available in your system.
2. Create a Hash
The HSET command can be used to create a Redis hash. The HSET command takes as arguments the hash name, the field name, and the field value.
127.0.0.1:6379> HSET testhashkey location india arch x86 memory 64GB
(integer) 3
127.0.0.1:6379>
3. Get a Hash content
The HGETALL command can be used to retrieve the content of a Redis hash. The HGETALL command accepts the hash's name as an argument and returns a list of field-value pairs.
127.0.0.1:6379> HGETALL testhashkey
1) "location"
2) "india"
3) "arch"
4) "x86"
5) "memory"
6) "64GB"
127.0.0.1:6379>
4. Get the value of a specific Hash field
The HGET command in Redis is used to retrieve the value of a hash field. It takes the hash name and the field name as arguments and returns the value of the field, or nil if the field does not exist.
127.0.0.1:6379> HGET testhashkey location
"india"
127.0.0.1:6379>
5. Identify if a Hash field exists.
In Redis, the HEXISTS command is used to determine whether a hash field exists. It receives the hash name and the field name as arguments and returns an integer value. 1 if field is exists in hash and 0 if field is not exists in hash.
127.0.0.1:6379> HEXISTS testhashkey memory
(integer) 1
127.0.0.1:6379>
6. Get all the fields in a Hash The HKEYS command in Redis can be used to get all the fields in a Hash. The HKEYS command accepts the Hash's name as an argument and returns a list of field names, or an empty list, if the Hash does not exist.
127.0.0.1:6379> HKEYS testhashkey
1) "location"
2) "arch"
3) "memory"
127.0.0.1:6379>
7. Get all the values in a hash
The HVALS command in Redis can be used to get all the values in a Hash. The HVALS command accepts the Hash's name as an argument and returns a list of value names, or an empty list, if the Hash does not exist.
127.0.0.1:6379> HVALS testhashkey
1) "india"
2) "x86"
3) "64GB"
127.0.0.1:6379>
8. Delete field from Redis Hash
The HDEL command can be used to delete a Redis hash. The HDEL command takes as arguments the hash name and the field names and deletes the specified fields from the hash.
127.0.0.1:6379> HDEL testhashkey memory
(integer) 1
127.0.0.1:6379> HGETALL testhashkey
1) "location"
2) "india"
3) "arch"
4) "x86"
127.0.0.1:6379>
9. Delete Redis Hash
You can use the DEL command to delete the entire hash. The DEL command accepts the key's name as an argument and deletes both the key and its associated value
127.0.0.1:6379> DEL testhashkey
(integer) 1
127.0.0.1:6379>
Kommentare