AOF持久化
就是把命令按照原始的文本存储到文件中,在从新启动的时候再一条条的执行。java
好比命令 set msg hello :git
3\r\n$3\r\nmsg\r\n$5\r\nhello\r\n
AOF文件自己记录了不少的命令,可是随着时间流逝,可能不少命令都重复了,redis能够经过手动 bgrewriteaof
或者配置自动执行 AOF重写来精简AOF文件github
Code.SLICE.source("buf[0] = '*';" +
" len = 1+ll2string(buf+1,sizeof(buf)-1,argc);" +
" buf[len++] = '\r';" +
" buf[len++] = '\n';" +
" dst = sdscatlen(dst,buf,len);" +
" for (j = 0; j < argc; j++) {" +
" o = getDecodedObject(argv[j]);" +
" buf[0] = '$';" +
" len = 1+ll2string(buf+1,sizeof(buf)-1,sdslen(o->ptr));" +
" buf[len++] = '\r';" +
" buf[len++] = '\n';" +
" dst = sdscatlen(dst,buf,len);" +
" dst = sdscatlen(dst,o->ptr,sdslen(o->ptr));" +
" dst = sdscatlen(dst,\"\r\n\",2);" +
" decrRefCount(o);" +
" }")
.interpretation("按照必定的格式转化命令,放到要存放的目的地")
.interpretation("1: 先计算命令一共有几个字符 ,好比命令 set msg hello ,那么首先写入的就是 3,而后追加 \r\n")
.interpretation("2: 遍历每一个字符,先写下$符号,而后记下这个字符的长度,追加 \r\n再记下长度和字符自己,而后追加 \r\n")
.interpretation("3: set msg hello 最终须要追加的内容为 3\r\n$3\r\nmsg\r\n$5\r\nhello\r\n");
复制代码
//..
Code.SLICE.source("while(1) ")
.interpretation("读取AOF文件的内容,按照 REPL 的格式 ,1条条命令处理");
//..
Code.SLICE.source("if (fgets(buf,sizeof(buf),fp) == NULL)")
.interpretation("从文件中读取必定字节到buf中");
//..
Code.SLICE.source("argc = atoi(buf+1);")
.interpretation("拿到命令的长度");
//..
Code.SLICE.source("for (j = 0; j < argc; j++) ")
.interpretation("一个个的解析这条命令的全部数据,中间会对写入的数据作校验");
//..
Code.SLICE.source("argv[j] = createObject(OBJ_STRING,argsds);")
.interpretation("将数据存储到argv数组");
//..
Code.SLICE.source("cmd = lookupCommand(argv[0]->ptr);")
.interpretation("确保要执行的命令是合法的redis命令");
//..
Code.SLICE.source("cmd->proc(fakeClient);")
.interpretation("模拟执行");
复制代码
能实现秒级的实时持久化redis
AOF与定时写入文件源码追踪
AOF重写源码追踪
启动加载源码追踪
Redis设计与实现
Redis开发与运维数组