对象 redis中用对象来表示数据库中的键值,每一个键值对由两个对象(键对象和值对象)表示。redis中的键对象存在键空间(一个字典)中,与值对象相对应。
对象由redisObject结构表示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 typedef struct redisObject { unsigned type:4 ; unsigned encoding:4 ; unsigned lru:LRU_BITS; int refcount; void *ptr; } robj;
如下图所示,redis对象共占16字节
类型(type) redis的对象类型共有5种,便是我们熟悉的字符串、列表、集合、有序集合和哈希。这五种在redis中对应为
1 2 3 4 5 #define OBJ_STRING 0 #define OBJ_LIST 1 #define OBJ_SET 2 #define OBJ_ZSET 3 #define OBJ_HASH 4
可使用TYPE key
命令获取对象类型,得到的是该key
对应的值对象的类型
1 2 3 4 5 redis> SADD numbers 1 2 3 (integer) 3 redis> TYPE numbers set
编码(encoding) 编码表示的是对象的底层数据结构,在redis中有如下编码
1 2 3 4 5 6 7 8 9 10 11 #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
可使用OBJECT ENCODING key
命令查看key
对应值对象的编码
1 2 3 4 5 redis> SADD numbers 1 2 3 (integer) 3 redis> OBJECT ENCODING numbers "intset"