一、特点
hash是一个string类型的field和value的映射表,特别适合用于存储对象。如果hash的属性值被删除完,那么hash的key也会被redis删除。hash结构是可以压缩的,相比较N多个字段都单独存储成string类型来说,会比较节省内存。
二、操作
1)HDEL key field [field ...] 删除一个或多个哈希字段
summary: Delete one or more hash fields
since: 2.0.0
2)HEXISTS key field 确定一个哈希字段是否存在
summary: Determine if a hash field exists
since: 2.0.0
3)HGET key field 获取一个哈希字段的值
summary: Get the value of a hash field
since: 2.0.0
4)HGETALL key 获取散列中的所有字段和值
summary: Get all the fields and values in a hash
since: 2.0.0
5)HINCRBY key field increment 将哈希字段的整数值增加给定的数字
summary: Increment the integer value of a hash field by the given number
since: 2.0.0
6)HINCRBYFLOAT key field increment 将哈希字段的浮点值增加给定的数量
summary: Increment the float value of a hash field by the given amount
since: 2.6.0
7)HKEYS key 获取一个散列中的所有字段
summary: Get all the fields in a hash
since: 2.0.0
8)HLEN key 获取一个散列中的字段数
summary: Get the number of fields in a hash
since: 2.0.0
9)HMGET key field [field ...] 获取所有给定哈希字段的值
summary: Get the values of all the given hash fields
since: 2.0.0
10)HMSET key field value [field value ...] 将多个哈希字段设置为多个值
summary: Set multiple hash fields to multiple values
since: 2.0.0
11)HRANDFIELD key [count [WITHVALUES]] 从散列中获取一个或多个随机字段
summary: Get one or multiple random fields from a hash
since: 6.2.0
12)HSCAN key cursor [MATCH pattern] [COUNT count] 递增迭代哈希字段和相关值
summary: Incrementally iterate hash fields and associated values
since: 2.8.0
13)HSET key field value [field value ...] 设置哈希字段的字符串值
summary: Set the string value of a hash field
since: 2.0.0
14)HSETNX key field value 仅当一个哈希字段不存在时,设置该字段的值
summary: Set the value of a hash field, only if the field does not exist
since: 2.0.0
15)HSTRLEN key field 获取哈希字段值的长度
summary: Get the length of the value of a hash field
since: 3.2.0
16)HVALS key 获取哈希中的所有值
summary: Get all the values in a hash
since: 2.0.0
三、应用场景
1、购物车 ==》id为key,商品id为field,商品数量为value
2、存储对象 key-value