0
点赞
收藏
分享

微信扫一扫

Redis 6.0源码学习 RedisObject


文章目录

  • ​​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

5

Module

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_RAW

SDS

字符串

OBJ_ENCODING_INT

整型

字符串

OBJ_ENCODING_HT

哈希表

集合、有序集合、哈希表

OBJ_ENCODING_ZIPMAP

压缩表

未使用

OBJ_ENCODING_LINKEDLIST

链表

不再使用

OBJ_ENCODING_ZIPLIST

压缩列表

哈希表、有序集合

OBJ_ENCODING_INTSET

整型集合

集合

OBJ_ENCODING_SKIPLIST

跳表

有序集合

OBJ_ENCODING_EMBSTR

sds

字符串

OBJ_ENCODING_QUICKLIST

整型

列表

OBJ_ENCODING_STREAM

stream

stream

代码片段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 条评论