- 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;
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 */ #define OBJ_STRING 0 #define OBJ_LIST 1 #define OBJ_SET 2 #define OBJ_ZSET 3 #define OBJ_HASH 4 #define OBJ_MODULE 5 #define OBJ_STREAM 6编码格式
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 #define OBJ_ENCODING_INT 1 #define OBJ_ENCODING_HT 2 #define OBJ_ENCODING_ZIPMAP 3 #define OBJ_ENCODING_LINKEDLIST 4 #define OBJ_ENCODING_ZIPLIST 5 #define OBJ_ENCODING_INTSET 6 #define OBJ_ENCODING_SKIPLIST 7 #define OBJ_ENCODING_EMBSTR 8 #define OBJ_ENCODING_QUICKLIST 9 #define OBJ_ENCODING_STREAM 10



