from: http://www.searchtb.com/2011/05/redis-storage.html
Redis: A persistent key-value database with built-in net interface written in ANSI-C for Posix systems
1 Redis 内存存储结构
本文是基于 Redis-v2.2.4 版本进行分析.
1.1 Redis 内存存储总体结构
Redis 是支持多key-value数据库(表)的,并用 RedisDb 来表示一个key-value数据库(表). redisServer 中有一个 redisDb *db; 成员变量, RedisServer 在初始化时,会根据配置文件的 db 数量来创建一个 redisDb 数组. 客户端在连接后,通过 SELECT 指令来选择一个 reidsDb,如果不指定,则缺省是redisDb数组的第1个(即下标是 0 ) redisDb. 一个客户端在选择 redisDb 后,其后续操作都是在此 redisDb 上进行的. 下面会详细介绍一下 redisDb 的内存结构.
redis 的内存存储结构示意图
redisDb 的定义:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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 *io_keys;
/* Keys with clients waiting for VM I/O */
dict *watched_keys;
/* WATCHED keys for MULTI/EXEC CAS */
int
id;
} redisDb;
struct
|
redisDb 中 ,dict 成员是与实际存储数据相关的. dict 的定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|