redis键的数据结构
redis值可能有很多种类型,但是redis的键类型都是一样的。都是一种叫redisObject的类型。
/* A redis object, that is a type able to hold a string / list / set */
/* The actual Redis Object */
#define REDIS_LRU_BITS 24
#define REDIS_LRU_CLOCK_MAX ((1<<REDIS_LRU_BITS)-1) /* Max value of obj->lru */
#define REDIS_LRU_CLOCK_RESOLUTION 1 /* LRU clock resolution in seconds */
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
看源码注释基本意思就是redisObject是redis真正的存储类型。可以用来存储string list set
解析一下该类型的实际含义
ype:对应值的redis类型。
/* Object types */
#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_ZSET 3
#define REDIS_HASH 4
encoding:为了空间效率和时间效率,对于同一种redis类型,redis可能采用不同的内部编码方式。这一步对用户来说是透明的。
/* 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 */
#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 */
lru:生存时间,默认1.5年。
refcount:该键值被引用次数。因为redis有共享对象的概念,之前提到到0-9999字符串还有redis命令自带的一些字符串例如“\r\n”就是共享对象,目前也只是支持字符串共享对象。
ptr指向值所在内存的地址。
执行命令将键值对存储到redis对应数据结构中。
key存储的是键对应的redisObject的地址。
value存储的就是对应的redisObject。
如执行list 的命令lpush listname content ,redisDb 里面,存放的一条记录key 为内容listname 的字符串,而value(此value 不是命令对应的value )存储的则是一个redisObject (type= REDIS_ENCODING_ZIPLIST ,prt=ziplist ),然后把content 对应的内容add 到ziplist 上。