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

创建一个键,其关联的值是由提供的序列化值(Serialized value)反序列化(Deserialize)获得,该序列化值可以由命令 DUMP 获得。

Create a key associated with a value that is obtained by deserializing the provided serialized value (obtained via DUMP).

如果 TTL0,则创建的键不存在到期时间,否则指定的到期时间(以毫秒为单位)将被设置。

If ttl is 0 the key is created without any expire, otherwise the specified expire time (in milliseconds) is set.

当前命令会校验 RDB 版本号及数据总和核验码(Checksum)。如果不匹配,将会返回一个错误。

RESTORE checks the RDB version and data checksum. If they don't match an error is returned.
  • 返回响应字符串,如果成功则返回 OK

    The command returns OK on success.
  • redis> DEL mykey
    0
    redis> RESTORE mykey 0 "\n\x17\x17\x00\x00\x00\x12\x00\x00\x00\x03\x00\
                            x00\xc0\x01\x00\x04\xc0\x02\x00\x04\xc0\x03\x00\
                            xff\x04\x00u#<\xc0;.\xe9\xdd"
    OK
    redis> TYPE mykey
    list
    redis> LRANGE mykey 0 -1
    1) "1"
    2) "2"
    3) "3"
  • 版本支持

    2.6.0+

    时间复杂度(Time complexity)

    O(1) 用于创建新键,额外的 O(N*M) 用于重构序列化值。

    O(1) to create the new key and additional O(N*M) to recostruct the serialized value, 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). However for sorted set values the complexity is O(N*M*log(N)) because inserting values into sorted sets is O(log(N)).