Redis从入门到放弃04——Hash数据类型

75 阅读4分钟

Redis从入门到放弃04——Hash数据类型

Hash 数据类型介绍

Introduction to Redis hashes

Redis hashes are record types structured as collections of field-value pairs. You can use hashes to represent basic objects and to store groupings of counters, among other things While hashes are handy to represent objects, actually the number of fields you can put inside a hash has no practical limits (other than available memory), so you can use hashes in many different ways inside your application.


Redis 哈希是由字段值对组成的记录类型。您可以使用hash数据类型来表示基本对象和存储计数器分组等 虽然hash数据类型可以方便地表示对象,但实际上您可以放入hash数据类型中的字段数量没有实际限制(除了可用内存),因此您可以在应用程序中以许多不同的方式使用hash数据类型。

limits (限制)

Every hash can store up to 4,294,967,295 (2^32 - 1) field-value pairs. In practice, your hashes are limited only by the overall memory on the VMs hosting your Redis deployment.


每个hash数据类型的字段最多可以存储4,294,967,295(2^32 - 1)个字段值对。在实践中,你的哈希值只受Redis部署的虚拟机的总内存的限制。

常用 Hash 命令

查看Hash数据类型下所有命令 :help @Hash

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0

  HEXISTS key field
  summary: Determine if a hash field exists
  since: 2.0.0

  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0

  HGETALL key
  summary: Get all the fields and values in a hash
  since: 2.0.0

  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0

  HINCRBYFLOAT key field increment
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0

  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0

  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0

  HMGET key field [field ...]
  summary: Get the values of all the given hash fields
  since: 2.0.0

  HMSET key field value [field value ...]
  summary: Set multiple hash fields to multiple values
  since: 2.0.0

  HRANDFIELD key [count [WITHVALUES]]
  summary: Get one or multiple random fields from a hash
  since: 6.2.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0

  HSET key field value [field value ...]
  summary: Set the string value of a hash field
  since: 2.0.0

  HSETNX key field value
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0

  HSTRLEN key field
  summary: Get the length of the value of a hash field
  since: 3.2.0

  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0

HSET key field value [field value ...] :将hash表 key 中的字段 field 的值设为 value, 并返回设置的字段个数

127.0.0.1:6379> hset myhash f1 v1 f2 v2 f3 v3
(integer) 3

HMSET key field value [field value ...]: 奖hash 表 key 中的字段 field 的值设置成 value ,返回是否成功的提示

127.0.0.1:6379> hmset myhash2 m1 v1 m2 v2 m3 v3
OK

HVALS key:返回hash表 key 中所有字段对应的vale值

127.0.0.1:6379> hvals myhash
1) "v1"
2) "v2"
3) "v3"

HGET key field:获取hash表 key 中 field 字段的值

127.0.0.1:6379> hget myhash f1
"v1"

HGET key field:获取hash表 key 中一个或多个字段的值

127.0.0.1:6379> hmget myhash f1 f2 f3
1) "v1"
2) "v2"
3) "v3"

HGETALL key:获取hash表 key 中所有的字段和其对应的值

127.0.0.1:6379> hset myhash f1 v1 f2 v2 f3 v3
(integer) 3
127.0.0.1:6379> hgetall myhash
1) "f1"
2) "v1"
3) "f2"
4) "v2"
5) "f3"
6) "v3"

HKEYS key:返回hash表 key 中所有的字段

127.0.0.1:6379> hkeys myhash
1) "f1"
2) "f2"
3) "f3"

HSTRLEN key field: 返回hash表 key 中字段对应value值的长度

127.0.0.1:6379> hvals myhash
1) "v11"
2) "v2"
3) "v3"
127.0.0.1:6379> hstrlen myhash f1
(integer) 3

HLEN key:返回hash表 key 中所有字段

127.0.0.1:6379> hkeys myhash
1) "f1"
2) "f2"
3) "f3"

HEXISTS key field :判断hash表 key 中是否存在某个字段

--- 存在返回1
127.0.0.1:6379> hexists myhash f1
(integer) 1
---不存在返回0
127.0.0.1:6379> hexists myhash f11
(integer) 0

HSETNX key field value:将hash表 key 中的字段 field 的值设为 value, 并返回设置的字段个数;仅当field不存在的时候才能设置成功

127.0.0.1:6379> hvals myhash
1) "v11"
2) "v2"
3) "v3"
127.0.0.1:6379> hsetnx myhash f1 v2
(integer) 0
127.0.0.1:6379> hvals myhash
1) "v11"
2) "v2"
3) "v3"

HINCRBY key field increment:为hash表 key 中的指定字段的整数值加上增量 increment ,返回该字段增加后的值

127.0.0.1:6379> hincrby h1 k3 1
(integer) 4

HINCRBYFLOAT key field increment:为hash表 key 中的指定字段的值加上浮点数值的增量 increment ,返回该字段增加后的值

127.0.0.1:6379> hincrbyfloat h1 k2 0.5
"2.5"

HRANDFIELD key [count [WITHVALUES]] :随机返回hash表中指定 count 数量的字段 ,如果加上 withvalues 参数则连同字段对应的value一起返回

127.0.0.1:6379> hrandfield h1
"k2"
127.0.0.1:6379> hrandfield h1 2
1) "k1"
2) "k2"
127.0.0.1:6379> hrandfield h1 2
1) "k3"
2) "k2"
127.0.0.1:6379> hrandfield h1 -1 withvalues
1) "k3"
2) "4"
--- 不会去重
127.0.0.1:6379> hrandfield h1 -10 withvalues
 1) "k3"
 2) "4"
 3) "k1"
 4) "2"
 5) "k1"
 6) "2"
 7) "k2"
 8) "2.5"
 9) "k1"
10) "2"
11) "k2"
12) "2.5"
13) "k2"
14) "2.5"
15) "k3"
16) "4"

HSCAN key cursor [MATCH pattern] [COUNT count]:迭代hash表中的键值对

127.0.0.1:6379> hscan h1 0 match k*
1) "0"
2) 1) "k1"
   2) "2"
   3) "k2"
   4) "2.5"
   5) "k3"
   6) "4"

HDEL key field [field ...] :删除hash表 key 中一个或者多个字段

127.0.0.1:6379> hdel h1 k1 k2 k3
(integer) 3
127.0.0.1:6379> hvals h1
(empty array)

应用场景

购物车新增商品: hset shopcar:uid1024 gid_334488 1 gid_334477 1 增加商品数量: hincrby shopcar:uid1024 gid_334477 1 获取商品总数: hlen shopcar:uid1024 全部选择: hgetall shopcar:uid1024