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

返回与指定键关联的 Redis 对象(Redis Object)的内部信息。

The OBJECT command allows to inspect the internals of Redis Objects associated with keys.

在调试(Debug)时,或试图搞明白所有键是否按你所指定的方式编码数据类型,以节省空间时,当前命令就变得非常有用。在使用 Redis 作为缓存时,你的应用程序也可以使用由当前命令提供的信息,来实现应用级别(Application level)的键回收策略(Key eviction policy)

It is useful for debugging or to understand if your keys are using the specially encoded data types to save space. Your application may also use the information reported by the OBJECT command to implement application level key eviction policies when using Redis as a Cache.

OBJECT 命令支持多种子命令:

  • OBJECT REFCOUNT <key> 返回与指定键关联的值的引用计数。该命令主要用于调试。
  • OBJECT ENCODING <key> 返回用于存储与指定键关联的值的内部存储方式。
  • OBJECT IDLETIME <key> 返回存储在指定键中的对象的空闲(未被任何读写操作所请求)时间( 以秒为单位)。虽然返回值以秒为单位,但该定时器的实际精确度(Resolution)10 秒,但在未来实现中可能有所不同。

OBJECT REFCOUNT <key> returns the number of references of the value associated with the specified key. This command is mainly useful for debugging.

OBJECT ENCODING <key> returns the kind of internal representation used in order to store the value associated with a key.

OBJECT IDLETIME <key> returns the number of seconds since the object stored at the specified key is idle (not requested by read or write operations). While the value is returned in seconds the actual resolution of this timer is 10 seconds, but may vary in future implementations.

对象可以用不同方式进行编码:

Objects can be encoded in different ways:

Strings can be encoded as raw (normal string encoding) or int (strings representing integers in a 64 bit signed interval are encoded in this way in order to save space).

Lists can be encoded as ziplist or linkedlist. The ziplist is the special representation that is used to save space for small lists.

Sets can be encoded as intset or hashtable. The intset is a special encoding used for small sets composed solely of integers.

Hashes can be encoded as zipmap or hashtable. The zipmap is a special encoding used for small hashes.

Sorted Sets can be encoded as ziplist or skiplist format. As for the List type small sorted sets can be specially encoded using ziplist, while the skiplist encoding is the one that works with sorted sets of any size.

一旦你进行某个操作使得 Redis 得知对应的可以节省空间的编码方式,所有特别指定的编码类型将会自动转换成一般类型(General type)

All the specially encoded types are automatically converted to the general type once you perform an operation that makes it impossible for Redis to retain the space saving encoding.
  • 不同的子命令将会返回不同的值:

    • 子命令 refcountidletime 返回整数。
    • 子命令 encoding 返回一个 bulk reply

    如果尝试检查的对象不存在,则返回 nil

    Different return values are used for different subcommands.

    Subcommands refcount and idletime return integers.

    Subcommand encoding returns a bulk reply.

    If the object you try to inspect is missing, a null bulk reply is returned.

  • redis> lpush mylist "Hello World"
    (integer) 4
    redis> object refcount mylist
    (integer) 1
    redis> object encoding mylist
    "ziplist"
    redis> object idletime mylist
    (integer) 10

    在下面这个例子中,你将可以看到,当 Redis 不能继续使用节省空间的编码方式时,对应的编码方式是如果改变的:

    redis> set foo 1000
    OK
    redis> object encoding foo
    "int"
    redis> append foo bar
    (integer) 7
    redis> get foo
    "1000bar"
    redis> object encoding foo
    "raw"
  • 版本支持

    2.2.3+

    时间复杂度(Time complexity)

    O(1)

    O(1) for all the currently implemented subcommands.