Redis系列(三):redisServer、redisDb、redisObject、sds四大结构体理解

前端之家收集整理的这篇文章主要介绍了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  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  redisObject {
unsigned type:4;
unsigned encoding:;
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).  refcount;
void *ptr;
} robj;

编码:

 

 类型:

 

 

4.sds解析

在C语言中都是用char数组来存放数据,在Redis中为了性能,封装了char

常用的一种结构体

 __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解决方案项目

 

 

 

 

猜你在找的Redis相关文章