一、命令的功能介绍
The CONFIG REWRITE command rewrites the redis.conf file the server was started with, applying the minimal changes needed to make it reflect the configuration currently used by the server, which may be different compared to the original one because of the use of the CONFIG SET command.
CONFIG REWRITE 命令对启动 Redis 服务器时所指定的 redis.conf 文件进行改写: 因为 CONFIG_SET
命令可以对服务器的当前配置进行修改, 而修改后的配置可能和 redis.conf 文件中所描述的配置不一样, CONFIG REWRITE
的作用就是通过尽可能少的修改, 将服务器当前所使用的配置记录到 redis.conf 文件中。
二、命令的功能特性
The rewrite is performed in a very conservative way:
Comments and the overall structure of the original redis.conf are preserved as much as possible.
If an option already exists in the old redis.conf file, it will be rewritten at the same position (line number).
If an option was not already present, but it is set to its default value, it is not added by the rewrite process.
If an option was not already present, but it is set to a non-default value, it is appended at the end of the file.
Non used lines are blanked. For instance if you used to have multiple save directives, but the current configuration has fewer or none as you disabled RDB persistence, all the lines will be blanked.CONFIG REWRITE is also able to rewrite the configuration file from scratch if the original one no longer exists for some reason. However if the server was started without a configuration file at all, the CONFIG REWRITE will just return an error.
Atomic rewrite processIn order to make sure the redis.conf file is always consistent, that is, on errors or crashes you always end with the old file, or the new one, the rewrite is performed with a single write(2) call that has enough content to be at least as big as the old file. Sometimes additional padding in the form of comments is added in order to make sure the resulting file is big enough, and later the file gets truncated to remove the padding at the end.
重写会以非常保守的方式进行:
* 原有 redis.conf 文件的整体结构和注释会被尽可能地保留。
* 如果一个选项已经存在于原有 redis.conf 文件中 , 那么对该选项的重写会在选项原本所在的位置(行号)上进行。
* 如果一个选项不存在于原有 redis.conf 文件中, 并且该选项被设置为默认值, 那么重写程序不会将这个选项添加到重写后的 redis.conf 文件中。
* 如果一个选项不存在于原有 redis.conf 文件中, 并且该选项被设置为非默认值, 那么这个选项将被添加到重写后的 redis.conf 文件的末尾。
* 未使用的行会被留白。 比如说, 如果你在原有 redis.conf 文件上设置了数个关于 save 选项的参数, 但现在你将这些 save 参数的一个或全部都关闭了, 那么这些不再使用的参数原本所在的行就会变成空白的。
* 即使启动服务器时所指定的 redis.conf 文件已经不再存在, CONFIG REWRITE 命令也可以重新构建并生成出一个新的
redis.conf 文件。
另一方面, 如果启动服务器时没有载入 redis.conf 文件, 那么执行 CONFIG REWRITE 命令将引发一个错误。