Redis 雜湊(Hash)

Redis 雜湊(Hash)

Redis hash 是一個 string 型別的 field(欄位) 和 value(值) 的對映表,hash 特別適合用於儲存物件。

Redis 中每個 hash 可以儲存 232 - 1 鍵值對(40多億)。

例項

127.0.0.1:6379>  HMSET itread01key name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
OK
127.0.0.1:6379>  HGETALL itread01key
1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"

在以上例項中,我們設定了 redis 的一些描述資訊(name, description, likes, visitors) 到雜湊表的 itread01key 中。


Redis hash 命令

下表列出了 redis hash 基本的相關命令:

序號命令及描述
1HDEL key field1 [field2]
刪除一個或多個雜湊表字段
2HEXISTS key field
檢視雜湊表 key 中,指定的欄位是否存在。
3HGET key field
獲取儲存在雜湊表中指定欄位的值。
4HGETALL key
獲取在雜湊表中指定 key 的所有欄位和值
5HINCRBY key field increment
為雜湊表 key 中的指定欄位的整數值加上增量 increment 。
6HINCRBYFLOAT key field increment
為雜湊表 key 中的指定欄位的浮點數值加上增量 increment 。
7HKEYS key
獲取所有雜湊表中的欄位
8HLEN key
獲取雜湊表中欄位的數量
9HMGET key field1 [field2]
獲取所有給定欄位的值
10HMSET key field1 value1 [field2 value2 ]
同時將多個 field-value (域-值)對設定到雜湊表 key 中。
11HSET key field value
將雜湊表 key 中的欄位 field 的值設為 value 。
12HSETNX key field value
只有在欄位 field 不存在時,設定雜湊表字段的值。
13HVALS key
獲取雜湊表中所有值。
14HSCAN key cursor [MATCH pattern] [COUNT count]
迭代雜湊表中的鍵值對。

更多命令請參考:https://redis.io/commands