配置

编辑 redis.conf 文件:

# 启用 AOF 持久化模式
appendonly yes

# 各种 redis 操作的工作目录
dir /home/vagrant/redis-home

# AOF 文件存放的文件夹名
appenddirname "appendonlydir"

# AOF 基准名称
# 结合上面的配置,文件将存放在于:
# /home/vagrant/redis-home/appendonlydir
#  - /home/vagrant/redis-home/appendonlydir/appendonly.aof.1.base.rdb
#  - /home/vagrant/redis-home/appendonlydir/appendonly.aof.1.incr.aof
#  - /home/vagrant/redis-home/appendonlydir/appendonly.aof.manifest
appendfilename "appendonly.aof"

# 写入频率,通常推荐 everysec
appendfsync everysec

文件分析

# appendonly.aof.manifest 是清单文件
# 会记载哪个是 rdb 类型的文件,哪个是 aof 类型的文件
# type b 即 .base.rdb
# type i 即 .incr.aof
[vagrant@Redis-1 appendonlydir]$ cat appendonly.aof.manifest
file appendonly.aof.1.base.rdb seq 1 type b
file appendonly.aof.1.incr.aof seq 1 type i
# appendonly.aof.1.base.rdb 是快照文件
# 在 aof 文件过大的时候,redis 会自动把过旧的数据合并构建成一个 rdb 类型的快照文件
# 让后再在 aof 文件中记载增量变化,这样可以缩小 aof 文件大小
[vagrant@Redis-1 appendonlydir]$ cat appendonly.aof.1.base.rdb | hexdump -C
00000000  52 45 44 49 53 30 30 31  31 fa 09 72 65 64 69 73  |REDIS0011..redis|
00000010  2d 76 65 72 05 37 2e 32  2e 34 fa 0a 72 65 64 69  |-ver.7.2.4..redi|
00000020  73 2d 62 69 74 73 c0 40  fa 05 63 74 69 6d 65 c2  |s-bits.@..ctime.|
00000030  d5 3b ba 65 fa 08 75 73  65 64 2d 6d 65 6d c2 70  |.;.e..used-mem.p|
00000040  37 0d 00 fa 08 61 6f 66  2d 62 61 73 65 c0 01 ff  |7....aof-base...|
00000050  00 d3 de 03 2c 42 96 75                           |....,B.u|
00000058
# appendonly.aof.1.incr.aof 是增量文件
# 记载每次写入操作的增量命令
# *2 代表有 2 个操作数 (SELECT 0)
# $6 代表命令长度为 6 (SELECT)
# $1 代表数据长度为 1 (0)
# 后半部分的 `set key value`` 以此类推
[vagrant@Redis-1 appendonlydir]$ cat appendonly.aof.1.incr.aof
*2
$6
SELECT
$1
0

...

*3
$3
set
$3
key
$5
value
Comments
Write a Comment