Redis哈希Hash鍵值對集合操作(查詢增加修改)
一、哈希 Hash 鍵值對集合
Redis 中的 Hash 數(shù)據(jù) 是一個 鍵值對集合 , 類似于 Java 中的 Map 集合 ;
Hash 數(shù)據(jù)底層數(shù)據(jù)結構是 :
- 壓縮列表 ZipList : Hash 中的 鍵值對 長度較短時 使用 壓縮列表 ;
- 哈希表 HashTable : Hash 中的 鍵值對 長度較長時 使用 哈希表 ;
Redis 中存儲對象的方式 :
存儲序列化之后的數(shù)據(jù) : 將 對象 序列化為 json 字符串 , 然后 存儲到 Redis 鍵值對 的 Value 值中 ;
- 如果要修改對象中的數(shù)據(jù) , 要 先將對象反序列化 , 然后修改對象中的值 , 最后將對象序列化并保存 ;
直接存儲對象字段 : 將每個對象的字段拆開 , 進行分開存儲 , 非常繁瑣 ;
- 每個 Redis 的 鍵 都保存一個 對象字段 , 一個對象可能要消耗多個 鍵 ;
使用 Hash 存儲 ( 推薦 ) : 將 對象 的 字段 , 都以 Hash 的 鍵值對 形式存儲起來 , 可以直接訪問修改對應的對象字段 ;
- 每個 Redis 鍵 保存一個對象 , 對象的屬性 由 Hash 鍵值對 保存 ;
鍵值對區(qū)分 : Redis 中的鍵值對 一般稱為 Key=Value , 在 Hash 中的鍵值對 一般稱為 Field=Value ;
二、查詢操作
1、Redis 中查詢 Hash 鍵值對數(shù)據(jù)
執(zhí)行
hget student name
命令 , 可以 獲取 Redis 中 student 鍵 對應的 Hash 數(shù)據(jù)中的 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 鍵是否存在
執(zhí)行
hexists student name
命令 , 可以 獲取 Redis 中 student 鍵 對應的 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 鍵 對應的 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 鍵 對應的 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 鍵對應值增減值
執(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、設置 Hash 中 Field 鍵對應值
執(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>

到此這篇關于Redis哈希Hash鍵值對集合操作(查詢增加修改)的文章就介紹到這了,更多相關Redis Hash鍵值操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
RedisDesktopManager無法遠程連接Redis的完美解決方法
下載RedisDesktopManager客戶端,輸入服務器IP地址,端口(缺省值:6379);點擊Test Connection按鈕測試連接,連接失敗,怎么回事呢?下面小編給大家?guī)砹薘edisDesktopManager無法遠程連接Redis的完美解決方法,一起看看吧2018-03-03
Redisson實現(xiàn)分布式鎖、鎖續(xù)約的案例
這篇文章主要介紹了Redisson如何實現(xiàn)分布式鎖、鎖續(xù)約,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
redis客戶端連接錯誤 NOAUTH Authentication required
本文主要介紹了redis客戶端連接錯誤 NOAUTH Authentication required,詳細的介紹了解決方法,感興趣的可以了解一下2021-07-07

