redis之Hash哈希類型以及存儲原理

1.概述

1.實際上主要是對一個對象的多重屬性(如人的姓名,性別,年齡)的存儲;

在這裏插入圖片描述

同樣是存儲字符串,Hash 與String 的主要區別?
1、把所有相關的值聚集到一個key 中,節省內存空間
2、只使用一個key,減少key 衝突
3、當需要批量獲取值的時候,只需要使用一個命令,減少內存/IO/CPU 的消耗

1.1Redis數據類型 中文官網 (不推薦,更新不及時)

http://www.redis.cn/topics/data-types-intro

1.2.Redis數據類型 英文官網 (推薦)

https://redis.io/topics/data-types-intro

2.Hash哈希類型的相關命令

2.1.命令參考地址:http://redisdoc.com/hash/hexists.html

2.2.設置key的單個field屬性值:hset gaoxinfu en_name frank

127.0.0.1:6379> hset gaoxinfu en_name frank
(integer) 1

已經存在的key,我們可以通過上面的命令繼續設置屬性值,比如我再設置一下年齡

127.0.0.1:6379> hset gaoxinfu age 18
(integer) 1

2.3.獲取key的單個field屬性值:hget gaoxinfu en_name

127.0.0.1:6379> hget gaoxinfu en_name
"frank"

2.4.設置key的多個field屬性值:hmset zhaobolun en_name brain age 8 sex m

設置zhaobolun這個人的英文名,年齡和性別等信息

127.0.0.1:6379> hmset zhaobolun en_name brain age 8 sex m
OK

2.5.獲取key的多個field屬性值:hget gaoxinfu en_name

127.0.0.1:6379> hmget zhaobolun en_name age sex
1) "brain"
2) "8"
3) "m"

2.6.獲取key的多個field屬性字段:hkeys zhaobolun

127.0.0.1:6379> hkeys zhaobolun
1) "en_name"
2) "age"
3) "sex"

2.7.獲取key的多個field屬性字段對應的value:hkeys zhaobolun

127.0.0.1:6379> HVALS zhaobolun
1) "brain"
2) "8"
3) "m"

2.8.獲取key的多個field屬性字段以及對應的value:HGETALL zhaobolun

127.0.0.1:6379> HGETALL zhaobolun
1) "en_name"
2) "brain"
3) "age"
4) "8"
5) "sex"
6) "m"
127.0.0.1:6379> 

2.9.獲取key的field屬性個數:HLEN zhaobolun

127.0.0.1:6379> HLEN zhaobolun
(integer) 3
127.0.0.1:6379> 

2.10.獲取key的field,並加上N值:HINCRBY zhaobolun age 2

127.0.0.1:6379> HKEYS zhaobolun
1) "en_name"
2) "age"
3) "sex"
127.0.0.1:6379> HGET zhaobolun  age
"8"
127.0.0.1:6379> HINCRBY zhaobolun age 2
(integer) 10
127.0.0.1:6379> 

2.11.獲取key的field,並減掉N值:HINCRBY zhaobolun age -2

127.0.0.1:6379> HINCRBY zhaobolun age -2
(integer) 8

2.12.刪除key對應的單個屬性:HDEL zhaobolun grade

127.0.0.1:6379> hset zhaobolun grade 3
(integer) 1
127.0.0.1:6379> hgetall zhaobolun
1) "en_name"
2) "brain"
3) "age"
4) "8"
5) "sex"
6) "m"
7) "grade"
8) "3"
127.0.0.1:6379> HDEL zhaobolun grade 
(integer) 1
127.0.0.1:6379> 

2.13.刪除key對應的多個屬性:HDEL zhaobolun sex age

127.0.0.1:6379> HDEL zhaobolun sex age
(integer) 2
127.0.0.1:6379> 

2.14.刪除key:DEL zhaobolun

127.0.0.1:6379> DEL zhaobolun
(integer) 1
127.0.0.1:6379> 

2.15.查看數據類型:type gaoxinfu

127.0.0.1:6379> hset gaoxinfu en_nmae frank sex m age 18
(integer) 2
127.0.0.1:6379> type gaoxinfu
hash
127.0.0.1:6379> 

3.Hash哈希類型存儲操作原理(略)

3.1.概述

1.首先我們redis的存儲結構就是Hash,本身也是一個KV 的結構,類似於Java中的HashMap,我們叫外層的哈希(Redis KV的實現),只用到了 hashtable
2.當存儲 hash 數據類型時, 我們把它叫做內層的哈希。
  內層的哈希底層可以使用兩種數據結構實現:
  一種:ziplist 即爲OBJ_ENCODING_ZIPLIST(壓縮列表),
  一種:hashtable 即爲OBJ_ENCODING_HT(哈希表),

3.2.ziplist類型數據結構

3.2.1.ziplist概述(源碼 ziplist.c)

/* The ziplist is a specially encoded dually linked list that is designed
 * to be very memory efficient. It stores both strings and integer values,
 * where integers are encoded as actual integers instead of a series of
 * characters. It allows push and pop operations on either side of the list
 * in O(1) time. However, because every operation requires a reallocation of
 * the memory used by the ziplist, the actual complexity is related to the
 * amount of memory used by the ziplist.
 */
 ziplist 是一個經過特殊編碼的雙向鏈表,它不存儲指向上一個鏈表節點和指向下一個鏈表節點的指針,而是存儲上一個節點長度和當前節點長度,
 通過犧牲部分讀寫性能,來換取高效的內存空間利用率,是一種時間換空間的思想。只用在字段個數少,字段值小的場景裏面。

在這裏插入圖片描述

3.2.2.ziplist 數據結構整體佈局

 *
 * The general layout of the ziplist is as follows:
 *
 * <zlbytes> <zltail> <zllen> <entry> <entry> ... <entry> <zlend>
 *
 * NOTE: all fields are stored in little endian, if not specified otherwise.
 *
 * <uint32_t zlbytes> is an unsigned integer to hold the number of bytes that
 * the ziplist occupies, including the four bytes of the zlbytes field itself.
 * This value needs to be stored to be able to resize the entire structure
 * without the need to traverse it first.
 *
 * <uint32_t zltail> is the offset to the last entry in the list. This allows
 * a pop operation on the far side of the list without the need for full
 * traversal.
 *
 * <uint16_t zllen> is the number of entries. When there are more than
 * 2^16-2 entries, this value is set to 2^16-1 and we need to traverse the
 * entire list to know how many items it holds.
 *
 * <uint8_t zlend> is a special entry representing the end of the ziplist.
 * Is encoded as a single byte equal to 255. No other normal entry starts
 * with a byte set to the value of 255.

3.3.hashtable類型數據結構

4.Hash哈希類型應用場景

4.1.String字符串類型可以做的,Hash類型都可以做

4.2.存儲對象類型數據,便於管理

1.比如以前,我們存儲某個對象,可能是一個表,但是現在一個key,然後不同的feild即可存儲
2.Hash類型比String類型能夠節省更多的key空間(以前一個對象,可能好多key去存儲,現在只需要一個),也更加便於集中管理

4.3.購物車

在這裏插入圖片描述

5.Hash哈希類型不適合的應用場景

1、Field 不能單獨設置過期時間
2、沒有bit 操作
3、需要考慮數據量分佈的問題(value 值非常大的時候,無法分佈到多個節點)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章