华域联盟 Redis Redis哈希Hash键值对集合操作(查询增加修改)

Redis哈希Hash键值对集合操作(查询增加修改)

目录

一、哈希 Hash 键值对集合

Redis 中的 Hash 数据 是一个 键值对集合 , 类似于 Java 中的 Map 集合 ;

Hash 数据底层数据结构是 :

  • 压缩列表 ZipList : Hash 中的 键值对 长度较短时 使用 压缩列表 ;
  • 哈希表 HashTable : Hash 中的 键值对 长度较长时 使用 哈希表 ;

Redis 中存储对象的方式 :

  • 存储序列化之后的数据 : 将 对象 序列化为 json 字符串 , 然后 存储到 Redis 键值对 的 Value 值中 ;

    • 如果要修改对象中的数据 , 要 先将对象反序列化 , 然后修改对象中的值 , 最后将对象序列化并保存 ;
  • 直接存储对象字段 : 将每个对象的字段拆开 , 进行分开存储 , 非常繁琐 ;

    • 每个 Redis 的 键 都保存一个 对象字段 , 一个对象可能要消耗多个 键 ;
  • 使用 Hash 存储 ( 推荐 ) : 将 对象 的 字段 , 都以 Hash 的 键值对 形式存储起来 , 可以直接访问修改对应的对象字段 ;

    • 每个 Redis 键 保存一个对象 , 对象的属性 由 Hash 键值对 保存 ;

键值对区分 : Redis 中的键值对 一般称为 Key=Value , 在 Hash 中的键值对 一般称为 Field=Value ;

二、查询操作

1、Redis 中查询 Hash 键值对数据

执行

hget student name

命令 , 可以 获取 Redis 中 student 键 对应的 Hash 数据中的 name 键 对应的 值 ;

代码示例 :

127.0.0.1:6379> hset student name Tom
(integer) 1
127.0.0.1:6379> get student
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> hget student
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget student name
"Tom"
127.0.0.1:6379>

注意 : 读取该 Hash 的 name=Tom 键值对 时 , 需要使用 hget student name 命令 ;

2、查询 Hash 键是否存在

执行

hexists student name

命令 , 可以 获取 Redis 中 student 键 对应的 Hash 数据中的 name 键 是否存在 ;

  • 如果存在 , 返回 1 ;
  • 如果不存在 , 返回 0 ;

代码示例 :

127.0.0.1:6379> hexists student name
(integer) 1
127.0.0.1:6379> hexists student name1
(integer) 0
127.0.0.1:6379>

3、查询 Hash 中所有的键 Field

执行

hkeys student

命令 , 可以 获取 Redis 中 student 键 对应的 Hash 数据中的 所有 键 Field ;

代码示例 :

127.0.0.1:6379> hkeys student
1) "name"
2) "age"
127.0.0.1:6379>

4、查询 Hash 中所有的值

执行

hvals student

命令 , 可以 获取 Redis 中 student 键 对应的 Hash 数据中的 所有 值 ;

代码示例 :

127.0.0.1:6379>
127.0.0.1:6379> hvals student
1) "Tom"
2) "18"
127.0.0.1:6379>

三、增加操作

1、Redis 中插入 Hash 键值对数据

执行

hset student name Tom

命令 , 可以 给 键 student 中的 Hash 数据值 中 添加 name=Tom 键值对 ;

代码示例 : 向 Redis 的 student 键值 下 插入 name=Tom 键值对 ;

127.0.0.1:6379> hset student name Tom
(integer) 1
127.0.0.1:6379> get student
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> hget student
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget student name
"Tom"
127.0.0.1:6379>

注意 : 读取该 Hash 的 name=Tom 键值对 时 , 需要使用 hget student name 命令 ;

2、批量插入 Hash 键值对数据

执行

hmset student name Tom age 18

命令 , 可以 给 键 student 中的 Hash 数据值 中 添加 name=Tom 和 age=18 键值对 ;

代码示例 : 向 Redis 的 student 键值 下 插入 name=Tom 和 age=18 键值对 ;

127.0.0.1:6379> hmset student name Tom age 18
OK
127.0.0.1:6379> hget student age
"18"
127.0.0.1:6379> hget student name
"Tom"
127.0.0.1:6379>

四、修改操作

1、Hash 中 Field 键对应值增减值

执行

hincrby student age -5

命令 , 可以 给 键 student 中的 Hash 数据值 中 age=18 数据中的值 -5 操作 ;

代码示例 :

127.0.0.1:6379>
127.0.0.1:6379> hincrby student age -5
(integer) 13
127.0.0.1:6379> hvals student
1) "Tom"
2) "13"
127.0.0.1:6379>

2、设置 Hash 中 Field 键对应值

执行

hsetnx student weight 85

命令 , 可以 在 键 student 中的 Hash 数据值 中 如果不存在 weight 键 , 则 添加 weight=85 键值对数据 ;

代码示例 :

127.0.0.1:6379>
127.0.0.1:6379> hsetnx student weight 85
(integer) 1
127.0.0.1:6379> hkeys student
1) "name"
2) "age"
3) "weight"
127.0.0.1:6379>

到此这篇关于Redis哈希Hash键值对集合操作(查询增加修改)的文章就介绍到这了,更多相关Redis Hash键值操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

本文由 华域联盟 原创撰写:华域联盟 » Redis哈希Hash键值对集合操作(查询增加修改)

转载请保留出处和原文链接:https://www.cnhackhy.com/157586.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们