Redis系列2-Redis底层数据结构
作者:互联网
RedisDB
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id; /* Database ID */
long long avg_ttl; /* Average TTL, just for stats */
unsigned long expires_cursor; /* Cursor of the active expire cycle. */
list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
} redisDb;
RedisObject
typedef struct redisObject {
unsigned type:4;
unsigned encoding:4;
unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
* LFU data (least significant 8 bits frequency
* and most significant 16 bits access time). */
int refcount;
void *ptr;
} robj;
- type:4位(bit),表示对象的类型
- encoding:4位(bit),表示对象的内部编码,Redis 可以根据不同的使用场景来为对象设置不同的编码,大大提高了 Redis 的灵活性和效率。
- lru: 24位(bit),高16位存储一个分钟数级别的时间戳,低8位存储访问计数(lfu : 最近访问次数)
- refcount: 记录的是该对象被引用的次数,类型为整型。refcount 的作用,主要在于对象的引用计数和内存回收。当对象的refcount>1时,称为共享对象
- ptr: 指针指向具体的数据
7种type
- 字符串对象sds
Redis 使用了 SDS(Simple Dynamic String)。用于存储字符串和整型数据。 - 跳跃表skiplist
- 字典dict
- 压缩列表ziplist
- 整数集合intset
- 快速列表quicklist
- 流对象stream
10种encoding
- intset
- hashtable
- int
- embstr
- raw
- quicklist
- ziplist
- skiplist
标签:keys,refcount,Redis,unsigned,对象,dict,数据结构,底层 来源: https://www.cnblogs.com/mingpingyao/p/15230515.html