Redis(四):基础篇 - 散列类型

散列类型(Hash)

  散列类型的键值是一种字典结构,其存储了字段(field)和字段值得映射,但字段值只能是字符串,不支持其他数据类型,换句话说,散列类型不能嵌套其他的数据类型。一个散列类型键可以包含至多 2³² - 1个字段。

提示 除了散列类型,Redis的其他数据类型同样不支持数据类型嵌套。比如集合类型的每个元素都只能是字符串,不能是另一个集合或散列表等。

  散列类型是和存储对象:使用对象类别和ID构成键名,使用字段表示对象的属性,而字段值则存储属性值。

命令

1、赋值与取值

HSET key field value
HGET key field
HMSET key field value [field value ...]
HMGET key field [field ...]
HGETALL key

HSET命令用来给字段复制,而HGET命令用来获取字段的值。如:

127.0.0.1:6379> HSET person name xiaoming
(integer) 1
127.0.0.1:6379> HSET person age 25
(integer) 1
127.0.0.1:6379> HGET person name
"xiaoming"

HSET命令的方便之处在于不区分插入和更新操作,这意味着修改数据时不用事先判断字段是否存在来决定要执行的是插入操作还是更新操作。当执行的是插入操作是,HSET命令会返回1,当执行的是更新操作是,HSET命令返回0.

127.0.0.1:6379> HSET person name zhangsan
(integer) 0
127.0.0.1:6379> HGET person name
"zhangsan"

当要同时设置多个字段的值时,可以使用 HMSET命令,同时获得多个字段可以使用HMGET,如:

127.0.0.1:6379> HMSET dog name pp age 3 color white
OK
127.0.0.1:6379> HMGET dog name age color
1) "pp"
2) "3"
3) "white"

如果想获取键中所有字段和字段值,却不知道键中有哪些字段是,可以使用 HGETALL,如:

127.0.0.1:6379> HGETALL dog
1) "name"
2) "pp"
3) "age"
4) "3"
5) "color"
6) "white"
127.0.0.1:6379> 

2、判断字段是否存在

HEXISTS key field

HEXISTS 命令用来判断一个字段是否存在。存在返回1,否则返回0,如:

127.0.0.1:6379> HEXISTS dog name
(integer) 1
127.0.0.1:6379> HEXISTS dog action
(integer) 0

3、当字段不存在时赋值

HSETNX key field value

HSETNX 命令与HSET命令类似,区别在于如果字段已经存在,HSETNX 命令将不执行任何操作,如:

127.0.0.1:6379> HSETNX dog name wangwu
(integer) 0
127.0.0.1:6379> HGET dog name
"pp"

4、增加数字

HINCRBY key field increment

与字符串类型中的INCRBY类似,可以使字段值增加指定的整数。散列类型没有HINCR命令,但是可以通过HINCRBY key field 1来实现。

127.0.0.1:6379> HINCRBY dog age 20
(integer) 23

5、删除字段

HDEL key field 

HDEL 命令可以删除一个或多个字段,返回值是被删除的字段个数,如:

127.0.0.1:6379> HDEL dog age
(integer) 1
127.0.0.1:6379> HDEL dog age
(integer) 0

6、只获取字段名或字段值

HKEYS key
HVALS key

仅仅获取键中的所有字段的名或者字段值,如:

127.0.0.1:6379> HKEYS dog
1) "name"
2) "color"
127.0.0.1:6379> HVALS dog
1) "pp"
2) "white"

7、获得字段数量

HLEN key

获取字段数量,如:

127.0.0.1:6379> HLEN dog
(integer) 2

官方提供

127.0.0.1:6379> 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


author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 sph1573@163.com,鄙人看到会及时回复

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值