Redis Hash
A Redis hash is a mapping table of string-type fields and values; hash is especially suitable for storing objects.
Each hash in Redis can store 2^32 - 1 key-value pairs (over 4 billion).
Example
127.0.0.1:6379> HMSET runoobkey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
OK
127.0.0.1:6379> HGETALL runoobkey
1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"
In the example above, we set some description information of redis (name, description, likes, visitors) into the hash runoobkey it.
Redis hash Commands
The table below lists the basic redis hash related commands:
| No. | Command and Description |
|---|---|
| 1 | HDEL key field1 [field2] |
| Delete one or more hash fields | |
| 2 | HEXISTS key field |
| Check whether the specified field exists in the hash key. | |
| 3 | HGET key field |
| Get the value of the specified field stored in the hash. | |
| 4 | HGETALL key |
| Get all fields and values of the specified key in the hash | |
| 5 | HINCRBY key field increment |
| Add the increment to the integer value of the specified field in the hash key. | |
| 6 | HINCRBYFLOAT key field increment |
| Add the increment to the floating-point value of the specified field in the hash key. | |
| 7 | HKEYS key |
| Get all fields in the hash | |
| 8 | HLEN key |
| Get the number of fields in the hash | |
| 9 | HMGET key field1 [field2] |
| Get the values of all given fields | |
| 10 | HMSET key field1 value1 [field2 value2 ] |
| Set multiple field-value pairs into the hash key at once. | |
| 11 | HSET key field value |
| Set the value of field in the hash key to value. | |
| 12 | HSETNX key field value |
| Set the value of the hash field only if the field does not exist. | |
| 13 | HVALS key |
| Get all values in the hash. | |
| 14 | HSCAN key cursor [MATCH pattern] [COUNT count] |
| Iterate over the key-value pairs in the hash. |
For more commands, please refer to:https://redis.io/commands