0%

Redis源码剖析之数据库结构

以下涉及到的源码均为redis5.0-rc3版本的代码【点击查看官方源码

Redis服务器初始化数据库的个数为16个,由server.h头文件中的宏定义而来,如下所示:

1
#define CONFIG_DEFAULT_DBNUM     16

并且每个数据库的结构都有一个redisDb构成,如下是redisDb的数据结构定义(server.h头文件中),各字段的含义可见上面的源码注释:

1
2
3
4
5
6
7
8
9
10
11
12
13
/* Redis database representation. There are multiple databases identified
* by integers from 0 (the default database) up to the max configured
* database. The database number is the 'id' field in the structure. */
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 */
list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
} redisDb;