redis数据库结构

2018-05-03  本文已影响0人  涵仔睡觉

数据库结构,每个redisDb代表一个数据库,默认情况下有16个数据库(0-15号数据库)。默认情况下使用0号数据库,可以使用SELECT命令切换数据库。

image
/* 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 */
    // 键的过期时间,字典的键为键,字典的值为过期事件 UNIX 时间戳
    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 */
    struct evictionPoolEntry *eviction_pool;    /* Eviction pool of keys */
    // 数据库号码
    int id;                     /* Database ID */
    // 数据库的键的平均 TTL ,统计信息
    long long avg_ttl;          /* Average TTL, just for stats */
} redisDb;

dict:数据库键空间,保存了数据库中所有的键值对。键为数据库的键,是一个字符串对象;值是数据库的值,可以是字符串对象、列表对象、哈希表对象、集合对象和有序集合对象中的一种。
expires:过期字典,保存了dict中所有键值对的过期时间,键是指向键空间(dict)中某个键对象的指针(不会造成内存浪费),值是一个longlong型的整数,保存了对应键的过期时间。
过期键删除策略:

redis采用惰性删除和定时删除两种策略结合。定时删除策略默认规定时间内检查16个数据库,每隔数据库检查20个键是否过期。

上一篇 下一篇

猜你喜欢

热点阅读