Redis源码解析1
Redis源码解析1 - 程序框架<div class="postbody"><div id="cnblogs_post_body">前言
Redis(REmote DIctionary Server)是一个由Salvatore Sanfilippo写的key-value存储系统。
它有以下特点:
[*]性能极高 &ndash; Redis能支持超过 100K+ 每秒的读写频率
[*]丰富的数据类型 &ndash; Redis支持二进制兼容的 string、list、set、zset、hash 等数据结构
[*]原子 &ndash; Redis的所有操作都是原子性的
[*]事务 - 支持多条指令合并成事务执行
[*]丰富的特性 &ndash; Redis还支持 publish/subscribe, 通知, key 过期等等特性
[*]脚本 - 新版本Redis支持lua脚本,可以实现更复杂的逻辑
总体结构
Redis是一个单线程的服务器(除了写磁盘会开子进程,VM管理会开线程,先忽略这两块)
所以,它的服务器启动流程非常清晰,看下图,不再赘述
http://pic002.cnblogs.com/images/2012/352836/2012091411064132.png
事件循环
1. 数据结构
通过 aeEventLoop 结构来表示一个事件框架,分析如下:
<div class="cnblogs_code"> 1 typedef struct aeEventLoop { 2 int maxfd; // 最大句柄号,只有select模型会用到 3 long long timeEventNextId; // timer事件的ID,初始化为0,使用时递增 4 aeFileEvent events; // 所有注册的事件,通过aeCreateFileEvent函数增加,aeDeleteFileEvent函数删除。注意,该数组以 fd 为下标进行存取 5 aeFiredEvent fired;// 当前帧激活事件,aeApiPoll函数将活跃事件加入到该数组。注意,该结构只保存fd,通过fd到events中取实际event指针 6 aeTimeEvent *timeEventHead; // timer事件链表 7 int stop; 8 void *apidata; /* This is used for polling API specific data */ 9 aeBeforeSleepProc *beforesleep;10 } aeEventLoop;
页:
[1]