一、哈希 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)的 值 ;
代碼示例 :文章來源:http://www.zghlxwxcb.cn/news/detail-554305.html
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>
文章來源地址http://www.zghlxwxcb.cn/news/detail-554305.html
到了這里,關(guān)于【Redis】Redis 哈希 Hash 鍵值對集合操作 ( 哈希 Hash 鍵值對集合簡介 | 查詢操作 | 增加操作 | 修改操作 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!