zoukankan      html  css  js  c++  java
  • 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解决方案项目

     

  • 相关阅读:
    itoa
    sprintf用法 [转载]
    atoi 与 itoa的实现
    数组排序总结(冒泡,选择,插入,希尔)
    XML入门精解之文件格式定义(DTD)
    malloc()函数的工作机制 结构体的总结
    字符串指针与字符数组(ZT)
    sprintf函数你了解有多深!
    sprintf
    sql ldr 笔记
  • 原文地址:https://www.cnblogs.com/vic-tory/p/13125227.html
Copyright © 2011-2022 走看看