redis3.0.7源码阅读(九)redis对象

来源:互联网 发布:最好的系统重装软件 编辑:程序博客网 时间:2024/05/01 11:23


版本:3.0.7


1.源文件
redis.h
object.c
t_hash.c
t_list.c
t_set.c
t_string.c
t_zset.c

2.数据结构
redis使用对象来表示数据库中的键和值(k/v),每创建一个键值对时,至少会创建两个对象,分别用于k/v,redis的键(k)总是字符串,值(v)则可以是字符串对象/列表对象/集合对象/有序集合对象/哈希对象,每种类型的对象又至少使用了两种不同的编码。举个例子,
127.0.0.1:6379> hset a b 1
(integer) 1
127.0.0.1:6379> type a
hash
127.0.0.1:6379> object encoding a
"ziplist"

/* * 对象类型 * 通过TYPE命令可查看值的类型 *//* Object types */// 字符串对象#define REDIS_STRING 0// 列表对象#define REDIS_LIST 1// 集合对象#define REDIS_SET 2// 有序集合对象#define REDIS_ZSET 3// 哈希对象#define REDIS_HASH 4/* * 对象编码 * 通过OBJECT ENCODING命令可查看值的编码类型 *//* 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 REDIS_ENCODING_RAW 0     /* Raw representation */// 整数#define REDIS_ENCODING_INT 1     /* Encoded as integer */// 哈希表#define REDIS_ENCODING_HT 2      /* Encoded as hash table */// 压缩map#define REDIS_ENCODING_ZIPMAP 3  /* Encoded as zipmap */// 双端链表#define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */// 压缩表#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */// 整数集合#define REDIS_ENCODING_INTSET 6  /* Encoded as intset */// 跳跃表#define REDIS_ENCODING_SKIPLIST 7  /* Encoded as skiplist */// embstr编码的动态字符串#define REDIS_ENCODING_EMBSTR 8  /* Embedded sds string encoding *//* * Redis 对象 */typedef struct redisObject {        // 对象类型    unsigned type:4;        // 对象编码    unsigned encoding:4;        // 对象最后一次被访问的时间    unsigned lru:REDIS_LRU_BITS; /* lru time (relative to server.lruclock) */        // 引用计数    int refcount;        // 指向实际值的指针    void *ptr;} robj;
3.内存分布

4.对象编码
4.1 字符串对象的编码:
int/raw/embstr
4.2 列表对象的编码:
ziplist/linkedlist
4.3 哈希对象的编码
ziplist/hashtables
4.4 集合对象的编码
inset/hashtables
4.5 有序集合对象的编码
ziplist/skiplist

注1:redis在执行命令时,会先进行对象类型检查,对象类型检查通过后,再进行对象编码检查,根据编码类型调用处理函数。
注2:redis会对整数值的字符串对象进行共享,OBJECT ENCODING查看编码类型
注3:OBJECT IDLETIME查看键的空闲时间

原文出自:http://blog.csdn.net/daiyudong2020/article/details/54236690

End;

0 0
原创粉丝点击