redis 是一个非关系类型的数据库。里面的数据都是以键值对(key-value)形式来保存的。
值的总类一共有5种:字符串对象、列表对象、哈希对象、集合对象、有序集合对象。
typeof struct redisObejct {
// 对象的类型
unsigned type:4;
// 对象的编码
unsigned encoding:4;
// 底层的数据结构
void* ptr;
}
字符串对象
redis> set num 10086
编码: int、row、embstr
列表对象
redis> RPUSH num 1 "three" 5
编码:ziplist、linkedlist
ziplist(压缩列表形式)
当列表的数据达到513个的时候,数据就会变成链表
哈希对象
redis> HSET profile name "Tom"
redis> HSET profile age 25
redis> HSET profile career "Programmer"
编码:ziplist、hashtable
当保存当键或者值大于66字节当时候,会变成hashtable的编码
集合对象
redis> SADD num 1 3 5
redis> SADD fruits "apple" "banana" "cherry"
编码:intset、hashtable
intset编码
hashtable编码
有序集合对象
redis> ZADD price 8.5 apple 5.0 banana 6.0 cherry
编码:ziplist、skiplist
ziplist编码