redis3.0.7源码阅读(十)redis数据库

曾经终败给现在 2022-07-13 11:58 191阅读 0赞

版本:3.0.7

1.源文件

redis.h

db.c

2.数据结构

  1. /*
  2. * 客户端
  3. */
  4. typedef struct redisClient {
  5. ...
  6. // 当时连接使用的数据库
  7. redisDb *db;
  8. ...
  9. } redisClient;
  10. /*
  11. * 服务器
  12. */
  13. struct redisServer {
  14. ...
  15. // db数组
  16. redisDb *db;
  17. // db数量
  18. int dbnum; /* Total number of configured DBs */
  19. ...
  20. };
  21. struct evictionPoolEntry {
  22. unsigned long long idle; /* Object idle time. */
  23. sds key; /* Key name. */
  24. };
  25. /*
  26. * redis数据库
  27. */
  28. /* Redis database representation. There are multiple databases identified
  29. * by integers from 0 (the default database) up to the max configured
  30. * database. The database number is the 'id' field in the structure. */
  31. typedef struct redisDb {
  32. // 数据库键空间,保存着数据库中的所有键值对
  33. dict *dict; /* The keyspace for this DB */
  34. // 键的过期时间,字典的键为键,字典的值为过期的时间戳
  35. dict *expires; /* Timeout of keys with a timeout set */
  36. // 正处于阻塞状态的键
  37. dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
  38. // 可以解除阻塞的键
  39. dict *ready_keys; /* Blocked keys that received a PUSH */
  40. // 正在被 WATCH 命令监视的键
  41. dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
  42. // LRU淘汰候选池
  43. struct evictionPoolEntry *eviction_pool; /* Eviction pool of keys */
  44. // 数据库号码
  45. int id; /* Database ID */
  46. // 数据库的键的平均 TTL ,统计信息
  47. long long avg_ttl; /* Average TTL, just for stats */
  48. } redisDb;

3.内存分布

SouthEast

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;

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读