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開發與運維數組