本文内容为 http://www.redis.io/commands/dump 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
DUMP key

将存储在键 key 中的值按 Redis 指定格式序列化(Serialize)并将其返回给用户。

Serialize the value stored at key in a Redis-specific format and return it to the user.

返回值可以通过命令 RESTORE 重新合成回到一个 Redis 键中。

The returned value can be synthesized back into a Redis key using the RESTORE command.

序列化的格式是透明(Opaque)的且不存在标准(Non-standard),不过仍存在少数语义特性:

  • 包含可用于确保错误能够被检测出来的 64 位校验码(Checksum)。命令 RESTORE 在使用序列化值合成一个键之前,保证会校验该校验码。
  • 值被编码的格式与 RDB 所使用的相同。
  • 一个 RDB 版本号将被编码进被序列化值中,所以使用不兼容 RDB 格式的不同 Redis 版本将会拒绝处理该序列化值。

The serialization format is opaque and non-standard, however it has a few semantical characteristics:

It contains a 64-bit checksum that is used to make sure errors will be detected. The RESTORE command makes sure to check the checksum before synthesizing a key using the serialized value.

Values are encoded in the same format used by RDB.

An RDB version is encoded inside the serialized value, so that different Redis versions with incompatible RDB formats will refuse to process the serialized value.

序列化值中包含过期信息(Expire information)。可以使用命令 PTTL 来捕获当前值的存在时间。

The serialized value does NOT contain expire information. In order to capture the time to live of the current value the PTTL command should be used.

如果键 key 不存在,将会返回 nil

If key does not exist a nil bulk reply is returned.
  • 序列化后的值。

    The serialized value.
  • redis> SET mykey 10
    OK
    redis> DUMP mykey
    "\u0000\xC0\n\u0006\u0000\xF8r?\xC5\xFB\xFB_("
  • 版本支持

    2.6.0+

    时间复杂度(Time complexity)

    O(1) 访问指定键,额外的 O(N*M) 来序列化它。

    (1) to access the key and additional O(N*M) to serialized it, where N is the number of Redis objects composing the value and M their average size. For small string values the time complexity is thus O(1)+O(1*M) where M is small, so simply O(1).