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;

类型

  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. */

编码格式

  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 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值