【Redis】Redis 哈希 Hash 键值对集合操作 ( 哈希 Hash 键值对集合简介 您所在的位置:网站首页 Redis中的Hash修改键的值 【Redis】Redis 哈希 Hash 键值对集合操作 ( 哈希 Hash 键值对集合简介

【Redis】Redis 哈希 Hash 键值对集合操作 ( 哈希 Hash 键值对集合简介

2023-04-26 07:19| 来源: 网络整理| 查看: 265

文章目录

一、哈希 Hash 键值对集合二、查询操作1、Redis 中查询 Hash 键值对数据2、查询 Hash 键是否存在3、查询 Hash 中所有的键 Field4、查询 Hash 中所有的值三、增加操作1、Redis 中插入 Hash 键值对数据2、批量插入 Hash 键值对数据四、修改操作1、Hash 中 Field 键对应值增减值2、设置 Hash 中 Field 键对应值一、哈希 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>


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有