Redis哈希Hash鍵值對集合操作(查詢增加修改)
一、哈希 Hash 鍵值對集合
Redis 中的 Hash 數(shù)據(jù) 是一個 鍵值對集合 , 類似于 Java 中的 Map 集合 ;
Hash 數(shù)據(jù)底層數(shù)據(jù)結(jié)構(gòu)是 :
- 壓縮列表 ZipList : Hash 中的 鍵值對 長度較短時 使用 壓縮列表 ;
- 哈希表 HashTable : Hash 中的 鍵值對 長度較長時 使用 哈希表 ;
Redis 中存儲對象的方式 :
存儲序列化之后的數(shù)據(jù) : 將 對象 序列化為 json 字符串 , 然后 存儲到 Redis 鍵值對 的 Value 值中 ;
- 如果要修改對象中的數(shù)據(jù) , 要 先將對象反序列化 , 然后修改對象中的值 , 最后將對象序列化并保存 ;
直接存儲對象字段 : 將每個對象的字段拆開 , 進行分開存儲 , 非常繁瑣 ;
- 每個 Redis 的 鍵 都保存一個 對象字段 , 一個對象可能要消耗多個 鍵 ;
使用 Hash 存儲 ( 推薦 ) : 將 對象 的 字段 , 都以 Hash 的 鍵值對 形式存儲起來 , 可以直接訪問修改對應(yīng)的對象字段 ;
- 每個 Redis 鍵 保存一個對象 , 對象的屬性 由 Hash 鍵值對 保存 ;
鍵值對區(qū)分 : Redis 中的鍵值對 一般稱為 Key=Value , 在 Hash 中的鍵值對 一般稱為 Field=Value ;
二、查詢操作
1、Redis 中查詢 Hash 鍵值對數(shù)據(jù)
執(zhí)行
hget student name
命令 , 可以 獲取 Redis 中 student 鍵 對應(yīng)的 Hash 數(shù)據(jù)中的 name 鍵 對應(yīng)的 值 ;
代碼示例 :
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 鍵是否存在
執(zhí)行
hexists student name
命令 , 可以 獲取 Redis 中 student 鍵 對應(yīng)的 Hash 數(shù)據(jù)中的 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
執(zhí)行
hkeys student
命令 , 可以 獲取 Redis 中 student 鍵 對應(yīng)的 Hash 數(shù)據(jù)中的 所有 鍵 Field ;
代碼示例 :
127.0.0.1:6379> hkeys student 1) "name" 2) "age" 127.0.0.1:6379>
4、查詢 Hash 中所有的值
執(zhí)行
hvals student
命令 , 可以 獲取 Redis 中 student 鍵 對應(yīng)的 Hash 數(shù)據(jù)中的 所有 值 ;
代碼示例 :
127.0.0.1:6379> 127.0.0.1:6379> hvals student 1) "Tom" 2) "18" 127.0.0.1:6379>
三、增加操作
1、Redis 中插入 Hash 鍵值對數(shù)據(jù)
執(zhí)行
hset student name Tom
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 添加 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 鍵值對數(shù)據(jù)
執(zhí)行
hmset student name Tom age 18
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 添加 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 鍵對應(yīng)值增減值
執(zhí)行
hincrby student age -5
命令 , 可以 給 鍵 student 中的 Hash 數(shù)據(jù)值 中 age=18 數(shù)據(jù)中的值 -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、設(shè)置 Hash 中 Field 鍵對應(yīng)值
執(zhí)行
hsetnx student weight 85
命令 , 可以 在 鍵 student 中的 Hash 數(shù)據(jù)值 中 如果不存在 weight 鍵 , 則 添加 weight=85 鍵值對數(shù)據(jù) ;
代碼示例 :
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>
到此這篇關(guān)于Redis哈希Hash鍵值對集合操作(查詢增加修改)的文章就介紹到這了,更多相關(guān)Redis Hash鍵值操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redisson實現(xiàn)Redis分布式鎖的幾種方式
本文在講解如何使用Redisson實現(xiàn)Redis普通分布式鎖,以及Redlock算法分布式鎖的幾種方式的同時,也附帶解答這些同學(xué)的一些疑問,感興趣的可以了解一下2021-08-08Redis中?HyperLogLog數(shù)據(jù)類型使用小結(jié)
Redis使用HyperLogLog的主要作用是在大數(shù)據(jù)流(view,IP,城市)的情況下進行去重計數(shù),這篇文章主要介紹了Redis中?HyperLogLog數(shù)據(jù)類型使用總結(jié),需要的朋友可以參考下2023-03-03SpringBoot整合Redis入門之緩存數(shù)據(jù)的方法
Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API,下面通過本文給大家介紹下SpringBoot整合Redis入門之緩存數(shù)據(jù)的相關(guān)知識,感興趣的朋友一起看看吧2021-11-11