Redis數(shù)據(jù)結(jié)構(gòu)底層編碼轉(zhuǎn)換

1、string

string在redis中有三種編碼類型:int \rightarrow embstr \rightarrow raw

類型使用條件

int:存儲的值為 -2^63~2^63-1 之間的整數(shù)

embstr:存儲的值不屬于int(如超出int范圍的整數(shù),或者其它字符),且長度不超過44個字節(jié)

raw:存儲的值長度超過44字節(jié)

### 為了節(jié)省篇幅,刪除了部分命令結(jié)果和換行,下同
## int
>> set test -9223372036854775808  # -2^63 = -9223372036854775808
>> object encoding test
"int"
>> set test -9223372036854775809
>> object encoding test
"embstr"
>> set test 9223372036854775807  # 2^63-1 = 9223372036854775807
>> object encoding test
"int"
>> set test 9223372036854775808
>> object encoding test
"embstr"

## embstr
>> set test a
>> object encoding test
"embstr"
>> set test abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr #長度44
>> object encoding test
"embstr"
>> set test abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs
>> object encoding test
"raw"
編碼轉(zhuǎn)換
# append等修改操作會將類型轉(zhuǎn)換為raw,incr/decr等數(shù)值運算操作則會將類型轉(zhuǎn)換為int
>> set test 1
>> object encoding test
"int"
>> append test 1
>> object encoding test
"raw"
>> incr test
(integer) 12
>> object encoding test
"int"
>> set test x
>> object encoding test
"embstr"
>> append test 1
>> object encoding test
"raw"
>> del test
補充說明

1、為什么是44字節(jié)?

內(nèi)存分配器分配內(nèi)存的時候,一般是2、4、8、16、32、64,在embstr類型中,RedisObject結(jié)構(gòu)體占了16字節(jié),SDS8結(jié)構(gòu)體占了3字節(jié),字符串結(jié)尾"\0"占一字節(jié),故64-16-3-1=44。

2、list

3.0版本前l(fā)ist在redis中有兩種編碼類型:ziplist和linkedlist,3.0版本后只有quicklist

為什么不用ziplist:1、中間元素查找復雜度高 2、存在連鎖更新風險

quicklist優(yōu)點:控制了ziplist長度,從而降低了中間元素查找復雜度和連鎖更新的風險

相關(guān)參數(shù)
參數(shù)項 默認值 說明
list-max-ziplist-size -2 參數(shù)用于控制quicklist中每個ziplist最大大小,-2表示不超過8kb

3、 hash

Hash在redis中有兩種編碼類型:ziplist和hashtable

編碼轉(zhuǎn)換
# hash-max-ziplist-entries 使用ziplist時鍵值對最大數(shù)量
>> config set hash-max-ziplist-entries 3
"OK"
>> hset test 1 a 
>> hset test 2 b
>> hset test 3 c
>> object encoding test
"ziplist"
>> hset test 4 d
>> object encoding test
"hashtable"
>> config set hash-max-ziplist-entries 512
>> del test

# hash-max-ziplist-value 使用ziplist時值的最大長度
>> config set hash-max-ziplist-value 8
"OK"
>> hset test 1 abcdefgh # 長度8
>> object encoding test
"ziplist"
>> hset test 2 abcdefghi
>> object encoding test
"hashtable"
>> config set hash-max-ziplist-value 64
>> del test
相關(guān)參數(shù)
參數(shù)項 默認值 說明
hash-max-ziplist-entries 512 使用ziplist時鍵值對最大數(shù)量,超過此數(shù)量使用hashtable
hash-max-ziplist-value 64 使用ziplist時值的最大長度,值長度超過此數(shù)使用hashtable

4、set

set在redis中有兩種編碼類型:intset和hashtable

類型使用條件

intset:存儲的值為 -2^63~2^63-1 之間的整數(shù)

hashtable:非intset的其他情況

編碼轉(zhuǎn)換
# 插入不符合intset條件的值
>> sadd test -9223372036854775808  # -2^63
>> object encoding test
"intset"
>> sadd test -9223372036854775809
>> object encoding test
"hashtable"
>> del test
>> sadd test 9223372036854775807  # 2^63-1
>> object encoding test
"intset"
>> sadd test 9223372036854775808
>> object encoding test
"hashtable"
>> del test
>> sadd test 1  # 插入非整數(shù)測試
>> object encoding test
"intset"
>> sadd test a
>> object encoding test
"hashtable"
>> del test

# set-max-intset-entries  使用intset時元素最大數(shù)量
>> config set set-max-intset-entries 3
"OK"
>> sadd test 1 2 3
>> object encoding test
"intset"
>> sadd test 4
>> object encoding test
"hashtable"
>> config set set-max-intset-entries 512
>> del test
相關(guān)參數(shù)
參數(shù)項 默認值 說明
set-max-intset-entries 512 使用intset時元素最大數(shù)量,超過此數(shù)量使用hashtable

5、 zset

zset在redis中有兩種編碼類型:ziplist和skiplist

編碼轉(zhuǎn)換
# zset-max-ziplist-entries 使用ziplist時值的最大數(shù)量
>> config set zset-max-ziplist-entries 3
"OK"
>> zadd test 1 a 2 b 3 c
>> object encoding test
"ziplist"
>> zadd test 4 d
>> object encoding test
"skiplist"
>> config set zset-max-ziplist-entries 128
>> del test

# zset-max-ziplist-value 使用ziplist時值的最大長度
>> config set zset-max-ziplist-value 8
"OK"
>> zadd test 1 abcdefgh # 長度8
>> object encoding test
"ziplist"
>> zadd test 2 abcdefghi
>> object encoding test
"skiplist"
>> config set zset-max-ziplist-value 64
>> del test
相關(guān)參數(shù)
參數(shù)項 默認值 說明
zset-max-ziplist-entries 128 使用ziplist時值的最大數(shù)量,超過此數(shù)量使用skiplist
zset-max-ziplist-value 64 使用ziplist時值的最大長度,值長度超過此數(shù)使用skiplist
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容