redis3.0.7源码阅读(十)redis数据库
版本:3.0.7
1.源文件
redis.h
db.c
2.数据结构
/*
* 客户端
*/
typedef struct redisClient {
...
// 当时连接使用的数据库
redisDb *db;
...
} redisClient;
/*
* 服务器
*/
struct redisServer {
...
// db数组
redisDb *db;
// db数量
int dbnum; /* Total number of configured DBs */
...
};
struct evictionPoolEntry {
unsigned long long idle; /* Object idle time. */
sds key; /* Key name. */
};
/*
* redis数据库
*/
/* 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 */
// 正在被 WATCH 命令监视的键
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
// LRU淘汰候选池
struct evictionPoolEntry *eviction_pool; /* Eviction pool of keys */
// 数据库号码
int id; /* Database ID */
// 数据库的键的平均 TTL ,统计信息
long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
3.内存分布
4.一些特性
4.1 默认情况下,redis客户端连接的是0号数据库,可以通过命令切换,redisClient会记录当前客户端的目标数据库
4.2 redis使用一个独立的字典expires保存key的过期时间,使用三种过期删除策略,定时删除(定时器),惰性删除(访问时删除),定期删除(比如随机检查算法)
4.3 从服务即使发现键过期了,也不会主动删除,而是等待主节点通知
4.4 新生成的rdb和aof文件都不会包含过期的key,当一个过期键被删除时,会在aof文件追加DEL
原文出自: http://blog.csdn.net/daiyudong2020/article/details/54236705
End;
还没有评论,来说两句吧...