之前写了redis当中的底部实现以及几种类型的简单命令,今天来聊一聊redis当中的对象模型~~
redis为每种外部可以访问到的数据结构提供了一个叫做对象类型的抽象,其底层实现是基于之前咱们讲过的adlist,ziplist等等那一堆,其对外隐藏了这些细节转而实现大家都知道的在文档当中介绍的命令。其实,在我们在redis当中创建某个数据类型的时候,redis为我们至少创建了两个对象,一个对象是存储该数据类型的名称的字符串对象,另一个就是实际存储这个数据类型的对象了。
在redis当中,其每一个对象都用一个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;
其中type和encoding分别表示当前对象的种类以及编码类型,具体取值见下面的代码:
-
/* A redis object, that is a type able to hold a string / list / set */
-
/* The actual Redis Object */
-
#define OBJ_STRING 0
-
#define OBJ_LIST 1
-
#define OBJ_SET 2
-
#define OBJ_ZSET 3
-
#define OBJ_HASH 4
-
-
/* Objects encoding. Some kind of objects like Strings and Hashes can be
-
* internally represented in multiple ways. The 'encoding' field of the object
-
* is set to one of this fields for this object. */
-
#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 */
lru记录的是内存置换策略,refcount是引用当前对象的个数,ptr则指向底层的具体实现,具体的一些限制如下:
-
#define LRU_BITS 24
-
#define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
-
#define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
-
-
#define OBJ_SHARED_REFCOUNT INT_MAX
后续会单独介绍单独对象的实现方式~~~
阅读(9702) | 评论(0) | 转发(0) |