• Redis 6.0源码学习 RedisObject


    Redis 6.0源码学习 RedisObject

      Redis中key和value的类型都是redisObject,可见学习这个类型的重要性。

    数据结构

      redisObject(见代码片段1)中存储了类型信息、编码格式、lru信息、引用次数和内容指针。

    代码片段1 server.h中redisObject相关定义

    #define LRU_BITS 24
    
    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    类型

      type属性,表示Redis对象的类型,占用4位,可以表示16种不同的类型,目前包含如下类型。

    对象类型
    0字符串String
    1列表List
    2集合Set
    3有序集合Sorted set
    4哈希表Hash
    5Module
    6流Stream

    代码片段2 server.h中redisObject type相关定义

    * A redis object, that is a type able to hold a string / list / set */
    
    /* 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. */
    
    /* The "module" object type is a special one that signals that the object
     * is one directly managed by a Redis module. In this case the value points
     * to a moduleValue struct, which contains the object value (which is only
     * handled by the module itself) and the RedisModuleType struct which lists
     * function pointers in order to serialize, deserialize, AOF-rewrite and
     * free the object.
     *
     * Inside the RDB file, module types are encoded as OBJ_MODULE followed
     * by a 64 bit module type ID, which has a 54 bits module-specific signature
     * in order to dispatch the loading to the right module, plus a 10 bits
     * encoding version. */
    #define OBJ_MODULE 5    /* Module object. */
    #define OBJ_STREAM 6    /* Stream object. */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    编码格式

      encoding属性,表示对象内部存储的编码,在一定条件下,对象的编码可以在多个编码之间转化,长度占用4位,包含如下编码。

    encoding数据结构可存储对象类型
    OBJ_ENCODING_RAWSDS字符串
    OBJ_ENCODING_INT整型字符串
    OBJ_ENCODING_HT哈希表集合、有序集合、哈希表
    OBJ_ENCODING_ZIPMAP压缩表未使用
    OBJ_ENCODING_LINKEDLIST链表不再使用
    OBJ_ENCODING_ZIPLIST压缩列表哈希表、有序集合
    OBJ_ENCODING_INTSET整型集合集合
    OBJ_ENCODING_SKIPLIST跳表有序集合
    OBJ_ENCODING_EMBSTRsds字符串
    OBJ_ENCODING_QUICKLIST整型列表
    OBJ_ENCODING_STREAMstreamstream

    代码片段3 server.h中redisObject encoding相关定义

    #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 */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    深度学习 图像分割综述
    如何写出一篇好文章——不动笔就能学会写文章的训练法
    客户关系应该如何管理?
    ansible自动化管理
    Elasticsearch学习
    【C语言 数据结构】串 - 顺序
    【SpringBoot】90、SpringBoot中@Value(“${...}“)的使用细节
    list容器(20221117)
    003.Vim编辑器
    最新日本IT在留资格认定你需要知道的
  • 原文地址:https://blog.csdn.net/qq1620657419/article/details/126035115