Redis原始碼初識三 RedisObject

2021-10-12 02:20:53 字數 2762 閱讀 6491

redis所有型別,key都是sds儲存,value都是對應redisojbect

typedef

struct redisobject robj;

type

值型別(string、list、set、hash、zset),占用4個bit

**中定義:

/* the actual redis object */

#define obj_string 0

/* string object. */

#define obj_list 1

/* list object. */

#define obj_set 2

/* set object. */

#define obj_zset 3

/* sorted set object. */

#define obj_hash 4

/* hash object. */

encoding

編碼型別,值實際使用的資料結構,占用4個bit

**中定義:

#define obj_encoding_raw 0     

/* raw representation */

#define obj_encoding_int 1

/* encoded as integer */

#define obj_encoding_ht 2

/* encoded as hash table */

#define obj_encoding_zipmap 3

/* encoded as zipmap */

#define obj_encoding_linkedlist 4

/* no longer used: old list encoding. */

#define obj_encoding_ziplist 5

/* encoded as ziplist */

#define obj_encoding_intset 6

/* encoded as intset */

#define obj_encoding_skiplist 7

/* encoded as skiplist */

#define obj_encoding_embstr 8

/* embedded sds string encoding */

#define obj_encoding_quicklist 9

/* encoded as linked list of ziplists */

#define obj_encoding_stream 10

/* encoded as a radix tree of listpacks */

lru

資料最近訪問時間,24個bit

refcount

引用次數(值被引用的次數),4個bit

ptr指標,指向真實資料結構,8個bit

型別資料結構

含義string

int值是整數型別

embstr

簡單字串(44個位元組以內包含),連續的記憶體空間,記憶體一次訪問

加上redisobject其他字段,正好64byte,記憶體每次讀取64byte

rawsds字串,與redisobject不是連續空間

list

quicklist

雙端鍊錶+ziplist

hash

ziplist

ziplist實現hash

hashtable

元素個數超過512或值長度超過65byte,改用hashtable

setintset

整數集合

hashtable

存的值不是int或超過512個,轉為hashtable儲存

zset

ziplist

ziplist結構實現zset

skiplist

skiplist+dict實現zset(元素超過128個或值大於64byte時轉)

當使用字串型別時,如果值的長度小於44位時,會使用embstr型別資料結構,優點是所屬redisobject,共占用64byte,為記憶體一次讀取大小,可一次獲取,所以記憶體讀取快

#define obj_encoding_embstr_size_limit 44

robj *

createstringobject

(const

char

*ptr, size_t len)

如上原始碼,當小於等於44時,建立embstr物件,否則建立raw物件

44長度計算公式:

redisojbect占用

字段:type(4bit)+encoding(4bit)+lru(8bit+16bit)+refcount(4byte)+ptr(8byte)

換算:4bit+4bit+8bit+16bit+4byte+8byte=16byte

sds占用

字段:uint8_t len(1 byte)+uint8_t alloc(1byte)+flags(1byte)+1byte(\n結尾自動拼接)

換算:1byte+1byte+1byte+1byte = 4byte

sds中buf可存位元組

64-16-4=44byte

redis原始碼 (九)Redis

前些天主要看了redis底層依賴的一些資料結構和事件管理庫的 比較零散,但大體上了解了作者的設計思路.對不同規模 n 的資料採用不同的資料結構以實現對記憶體利用的 最優 這裡的最優我想作者也沒有做過嚴格的實驗,不同的應用場景在redis上的表現肯定有所不同,如果有必要,可以再配置檔案中對一些閾值做調...

linux系統下php原始碼安裝redis擴充套件

安裝redis 版本 2.2.4 進入 usr local src目錄 wget 2 解壓 安裝 tar zxvf phpredis 2.2.4.tar.gz cd phpredis 2.2.4 進入安裝目錄 usr local php bin phpize 用phpize生成configure配置...

Redis(三)跳躍表介紹及原始碼

查詢鍊錶的時間複雜度o n 即使該鍊錶的是有序的,但若我們在鍊錶上在加一層鏈,且每次跳過乙個節點 即進行一次二分 如下圖所示 若在l2鏈的基礎上增加一層鏈,在每次跳過l2上的乙個節點 即在l2鏈上進一步二分 那麼我便可以進一步增加搜尋速度,如下圖所示 此時查詢8只需在l3上搜尋2次,查詢7需要3次 ...