<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                2021-04-11 周天 ## 知識點 1. redis數據庫的持久化。 因為Redis是內存型數據庫,如果不將數據存到磁盤上,一旦程序退出或者斷電,數據就會丟失。 2. redis模型和事件。 ## rdb rdb是redis提供的一種持久化方式,是將redis內存快照存到dump.rdb文件中,重啟時可以從最新的快照文件恢復數據。 ### save和bgsave命令 * save命令會阻塞服務端進程,直至rdb文件生成結束。 * bgsave命令則是fork一個子進程去完成rdb文件的生成,不會阻塞服務端進程。 * save和bgsave在服務端不會同時執行。 * rdb文件在裝載時,服務器會處于阻塞狀態,直到裝載完成,才對外提供服務。 1. 自動間歇性保持rdb ``` # save <seconds> <changes> # Will save the DB if both the given number of seconds and the given # In the example below the behaviour will be to save: # Note: you can disable saving completely by commenting out all "save" lines. # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # save "" save 900 1 save 300 10 save 60 10000 ``` 只要滿足3個條件中的一個,bgsave就會被執行。 save的保存條件會存在redisServer下的saveparams參數里。 ``` long long dirty; /* 記錄數據庫狀態修改了多少次(rdb后會重置為0) */ long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */ time_t lastsave; /* save最后執行成功時間 */ time_t lastbgsave_try; /* bgsave最后執行成功時間 */ struct saveparam *saveparams; /* Save points array for RDB */ struct saveparam { time_t seconds; int changes; }; ``` 2. rdb文件結構 redis版本升級后,可能會對該結構做部分優化。(該結構是2.9版本) redis rdb是一個經過壓縮的二進制文件,包含多個部分,結構如下圖。對不不同類型的redis對象,會采取不同方式存儲。 ![](https://img.kancloud.cn/60/88/608845237501698fc26d23065e0de066_1416x752.png) 3. rdb相關演示 在6.0版本下演示結果: 先保存空的rdb文件,查看格式;接著在10數據庫里加入了一個kv,保存rdb文件后再次查看效果。 ``` shell [root@68df2d288502 6379]# od -c dump.rdb 0000000 R E D I S 0 0 0 9 372 \t r e d i s 0000020 - v e r 005 6 . 0 . 6 372 \n r e d i 0000040 s - b i t s 300 @ 372 005 c t i m e 302 0000060 321 342 w ` 372 \b u s e d - m e m 302 p 0000100 326 \r \0 372 \f a o f - p r e a m b l 0000120 e 300 \0 377 036 . 334 247 236 q > 277 0000134 [root@68df2d288502 6379]# redis-cli 127.0.0.1:6379> select 0 OK 127.0.0.1:6379> select 10 OK 127.0.0.1:6379[10]> DBSIZE (integer) 0 127.0.0.1:6379[10]> SADD KEY1 hello (integer) 1 127.0.0.1:6379[10]> DBSIZE (integer) 1 127.0.0.1:6379[10]> save OK 127.0.0.1:6379[10]> exit [root@68df2d288502 6379]# od -c dump.rdb 0000000 R E D I S 0 0 0 9 372 \t r e d i s 0000020 - v e r 005 6 . 0 . 6 372 \n r e d i 0000040 s - b i t s 300 @ 372 005 c t i m e 302 0000060 351 343 w ` 372 \b u s e d - m e m 302 ` 0000100 327 \r \0 372 \f a o f - p r e a m b l 0000120 e 300 \0 376 \n 373 001 \0 002 004 K E Y 1 001 005 0000140 h e l l o 377 200 354 } ] 214 345 * 0000156 ``` 4. 重點回顧 ![](https://img.kancloud.cn/4a/c6/4ac67c73d7928b3f203ae0a3b53f1502_1250x344.png) ## aof append only file,redis另外一種持久化方式,采取的是存儲客戶端執行命令來記錄數據庫狀態的。 1. aof的文件寫入及文件格式 ``` struct redisServer{ ... sds aof_buf; /* AOF buffer, written before entering the event loop */ ... } ``` 其中aof需要記錄的命令都會先存儲到aof_buf的sds數據結構中,等刷新到磁盤后清空掉已經寫入的。 開啟aof配置,并默認每秒刷新buf到磁盤策略。 ``` 1060 appendonly yes 1061 1062 # The name of the append only file (default: "appendonly.aof") 1063 1064 appendfilename "appendonly.aof" 1087 # If unsure, use "everysec". 1088 1089 # appendfsync always 1090 appendfsync everysec 1091 # appendfsync no ``` ![](https://img.kancloud.cn/ac/8e/ac8e17434d6c87659f9b2f6958fe8542_1280x1124.png) 2. aof文件的裝載 ![](https://img.kancloud.cn/cb/8a/cb8af68a270aff568145476e104f4a48_636x602.png) 3. aof文件重寫(瘦身) 隨著時間推移,aof文件會越來越大,所以redis會為aof文件做重寫處理。 比如重復執行新增k1和刪除k1的命令,如果成對出現,可以將其刪除掉,不影響最終數據庫狀態。 相關配置: ``` 1128 # Specify a percentage of zero in order to disable the automatic AOF 1129 # rewrite feature. 1130 1131 auto-aof-rewrite-percentage 100 1132 auto-aof-rewrite-min-size 64mb ``` 一旦滿足如上條件,redis就會fork一個子進程去做aof重寫。 或者通過命令`bgrewriteaof`來執行。 ***通過如下演示,得出結論,redis6.0.6里執行bgrewriteaof重寫aof后,aof文件內存的是和rdb一樣的二進制數據(不是分析原來的aof文件,而是直接rdb一次),后續的命令還是以文本方式追加。*** ``` shell [root@68df2d288502 6379]# cat appendonly.aof REDIS0009? redis-ver6.0.6? ?edis-bits?@?ctime?,?w`used-mem?h4 aof-preamble?? ? a key1xxxxx?(?%?iW[root@68df2d288502 6379]# [root@68df2d288502 6379]# [root@68df2d288502 6379]# ls appendonly.aof dump.rdb [root@68df2d288502 6379]# redis-cli 127.0.0.1:6379> DBSIZE (integer) 0 127.0.0.1:6379> select 10 OK 127.0.0.1:6379[10]> DBSIZE (integer) 2 127.0.0.1:6379[10]> set aaa 111 OK 127.0.0.1:6379[10]> get aaa "111" 127.0.0.1:6379[10]> INCR aaa (integer) 112 127.0.0.1:6379[10]> exit [root@68df2d288502 6379]# ll total 8 -rw-r--r-- 1 root root 202 Apr 15 08:46 appendonly.aof -rw-r--r-- 1 root root 124 Apr 15 08:38 dump.rdb [root@68df2d288502 6379]# cat appendonly.aof REDIS0009? redis-ver6.0.6? ?edis-bits?@?ctime?,?w`used-mem?h4 aof-preamble?? ? a key1xxxxx?(?%?iW*2 $6 SELECT $2 10 *3 $3 set $3 aaa $3 111 *2 $4 INCR $3 aaa [root@68df2d288502 6379]# redis-cli 127.0.0.1:6379> BGREWRITEAOF Background append only file rewriting started 127.0.0.1:6379> exit [root@68df2d288502 6379]# ll total 8 -rw-r--r-- 1 root root 131 Apr 15 08:47 appendonly.aof -rw-r--r-- 1 root root 124 Apr 15 08:38 dump.rdb [root@68df2d288502 6379]# cat appendonly.aof REDIS0009? redis-ver6.0.6? ?edis-bits?@?ctime′?w`used-mem?4 aof-preamble?? ?aaa?p a key1xxxxx??'a????[root@68df2d288502 6379]# cat dump.rdb REDIS0009? redis-ver6.0.6? redis-bits?@?ctime??w`used-mem?X? ? aof-preamble?? ? a key1xxxxx?8{A?\??g[root@68df2d288502 6379]# redis-cli 127.0.0.1:6379> save OK 127.0.0.1:6379> exit [root@68df2d288502 6379]# ll total 8 -rw-r--r-- 1 root root 131 Apr 15 08:47 appendonly.aof -rw-r--r-- 1 root root 131 Apr 15 08:48 dump.rdb [root@68df2d288502 6379]# ``` 4. 重點回顧 ![](https://img.kancloud.cn/d1/09/d1095b8728821f7e188f3805e4a578d8_1262x736.png) ## 事件 ![](https://img.kancloud.cn/b0/6f/b06f3e54314641794bc7b859a9df155b_1684x1250.png) * 重點回顧 ![](https://img.kancloud.cn/79/48/79487fa03d6b5676108ea20fd85adf50_1260x558.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看