Redis系列(三):redisServer、redisDb、redisObject、sds四大結構體理解


一.源碼下載:

Windows中的Redis源碼下載:https://github.com/microsoftarchive/redis/tree/3.2

根據官網說明可知,用VS2013編譯,但是必須更新到update5, 否則會出現各種編譯錯誤,確實如此,之前用vs2013的其它版本,出現各種錯誤,無法修改。

打開VS2013---幫助---關於,即可查看自己的VS版本,例如我重裝之后的update5:                                                                   

不是VS2013 update5的可以下載重裝。

vs2013 update5下載鏈接:http://www.121down.com/soft/softview-43319.html

打開RedisServer.sln 一共9個項目:

 

Linux中的Redis源碼:http://download.redis.io/releases/redis-6.0.5.tar.gz

二.整體示意圖:

 

 

 

 

 

 

三.源碼解析

1.redisServer 解析

通過main函數來初始化redisServer()

int main(int argc, char **argv) {
    struct timeval tv;
    int j;
    //.....
    initServer();
}

 

 監聽端口

 這里面的server代表的就是redisServer

 初始化db數目

 2.redisDb解析

typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
unsigned long expires_cursor; /* Cursor of the active expire cycle. */
list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
} redisDb;

 

 

 

3.redisObject解析

typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
* LFU data (least significant 8 bits frequency
* and most significant 16 bits access time). */
int refcount;
void *ptr;
} robj;

編碼:

 

 類型:

 

 

4.sds解析

在C語言中都是用char數組來存放數據,在Redis中為了性能,封裝了char

常用的一種結構體

struct __attribute__ ((__packed__)) sdshdr8 {
    uint8_t len; /* used */
    uint8_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};

 

 

 

注:有興趣的朋友可以通過cmake把redis編譯成vs解決方案項目

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM