美文网首页NoSQL数据库
Redis数据类型·散列类型hash

Redis数据类型·散列类型hash

作者: 技术老男孩 | 来源:发表于2023-03-22 09:26 被阅读0次

一、介绍:

  • 散列类型(hash)的键值也是一种字典结构,其存储了字段(field)和字段值的映射
  • 字段值只能是字符串
  • 散列类型适合存储对象。使用对象类别和 ID 构成键名,使用字段表示对象的属性,而字段值则存储属性值

二、命令

命令 说明
HSET h key value 散列赋值,相当于map字典
HGET h key 散列取值
HSET h k1 v1 k2 v2 散列赋多个值
HGETALL key 散列获取所有字段
HEXISTS h key 散列判断字段是否存在
0不存在
1存在
HSETNX h key value 当字段不存在时赋值
HINCRBY h key value 散列数字递增
HDEL h key 散列删除字段
HKEYS h 只获取字段名
HVALS h 只获取值
HLEN h 散列获得字段数量

三、散列类型实践

  • 例:将文章ID号为10的文章以散列类型存储在Redis中
127.0.0.1:6379> HSET post:10 title 例解Python
(integer) 1
127.0.0.1:6379> HGETALL post:10
1) "title"
2) "\xe4\xbe\x8b\xe8\xa7\xa3Python"
127.0.0.1:6379> HSET post:10 author ZhangZhiGang
(integer) 1
127.0.0.1:6379> HMSET post:10 date 2021-05-01 summary 'Python Programming'
OK
127.0.0.1:6379> HGETALL post:10
1) "title"
2) "\xe4\xbe\x8b\xe8\xa7\xa3Python"
3) "author"
4) "ZhangZhiGang"
5) "date"
6) "2021-05-01"
7) "summary"
8) "Python Programming"

相关文章

网友评论

    本文标题:Redis数据类型·散列类型hash

    本文链接:https://www.haomeiwen.com/subject/osirkdtx.html