redis是key-value存储系统,其中key类型一般为字符串,而value类型则为redis对象,即redisObject。
一、定义
- 官方定义(redis-6.0.7)
1 | # src/server.h |
type,对象类型,可取值为(高版本见server.h,低版本如2.6.12则定义在redis.h,且常量名不一样,下encoding属性同样):
- #define OBJ_STRING 0
- #define OBJ_LIST 1
- #define OBJ_SET 2
- #define OBJ_ZSET 3
- #define OBJ_HASH 4
encoding,编码方式,表示
ptr
指向的具体数据结构,redis对每一种数据类型都做了多种编码(<3.2,>=3.2除list外)以达到最大性能,可取值为:- #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 */
lru:表示对象最后一次被命令程序访问的时间
refcount:引用计数,C语言不具备自动内存回收功能,Redis在自己的对象系统中构建了一个基于引用计数实现的内存回收机制,通过这一机制程序可以通过追踪对象的引用计数信息,在适当的时候自动释放对象,并进行内存回收。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#object.c
void decrRefCount(robj *o) {
if (o->refcount == 1) {
switch(o->type) {
case OBJ_STRING: freeStringObject(o); break;
case OBJ_LIST: freeListObject(o); break;
case OBJ_SET: freeSetObject(o); break;
case OBJ_ZSET: freeZsetObject(o); break;
case OBJ_HASH: freeHashObject(o); break;
case OBJ_MODULE: freeModuleObject(o); break;
case OBJ_STREAM: freeStreamObject(o); break;
default: serverPanic("Unknown object type"); break;
}
zfree(o);
} else {
if (o->refcount <= 0) serverPanic("decrRefCount against refcount <= 0");
if (o->refcount != OBJ_SHARED_REFCOUNT) o->refcount--;
}
}ptr:指向对象的底层实现数据结构,这个数据结构由type属性和encoding属性共同决定。