w3resource

Redis Keys: OBJECT

Redis OBJECT Command

The Redis OBJECT command is used to inspect the internals of Redis Objects associated with keys. It is useful for debugging or to understand if your keys are using the specially encoded data types to save space.

The OBJECT command supports multiple sub commands:

  • 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. 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 or int.
  • 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.

Here are some common uses and scenarios for the "OBJECT" command:

  • Memory Usage: Check the memory usage of a specific key with OBJECT ENCODING and OBJECT IDLETIME.
  • Encoding Type: Determine the internal encoding used for a key (e.g., raw, intset).
  • Access Time: Retrieve the time since the last access to a key.
  • Debugging: Gain insights into key internals for debugging and optimization purposes.

Syntax:

OBJECT subcommand [arguments [arguments ...]]

Available since

2.2.3.

Return Value

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.

Example: Redis OBJECT refcount key

127.0.0.1:6379> SET key 0
OK
127.0.0.1:6379> OBJECT refcount key
(integer) 2
127.0.0.1:6379> SET key1 0
OK
127.0.0.1:6379> OBJECT refcount key1
(integer) 3
127.0.0.1:6379> SET key2 APPLE
OK
127.0.0.1:6379> OBJECT refcount key2
(integer) 1
127.0.0.1:6379> SET key3 APPLE
OK
127.0.0.1:6379> OBJECT refcount key3
(integer) 1

The usage OBJECT encoding key

encoding data type

  • Strings: int, embstr, raw
  • Lists: ziplist (small lists), linkedlist
  • Sets: intset (integers and small sets), hashtable
  • Sorted Sets: ziplist (small sets), skiplist
  • Hashes: zipmap (small hashes), hashtable

Strings data type: int (integer), embstr (small string), raw

  • int: value represents the integer. integer, real (including the decimal point) is classified as a string. If the number starts with a zero string. For example, 123 is int and is a 0123 embstr.
  • embstr: it indicates that the value is string and 39 characters or less. embedded string, embstr is displayed from 3.0. The previous version was shown as raw.
  • raw: value is a string that represents more than 40 characters.

Example: Redis OBJECT encoding

127.0.0.1:6379> SET key 123
OK
127.0.0.1:6379> OBJECT encoding key
"int"
127.0.0.1:6379> APPEND key Apple
(integer) 8
127.0.0.1:6379> OBJECT encoding key
"raw"
127.0.0.1:6379> SET key 1234
OK
127.0.0.1:6379> OBJECT encoding key
"int"
127.0.0.1:6379> SET key 0123
OK
127.0.0.1:6379> OBJECT encoding key
"embstr"
127.0.0.1:6379> SET key 20.34
OK
127.0.0.1:6379> OBJECT encoding key
"embstr"
127.0.0.1:6379> SET key 012345678901234567890123456789
OK
127.0.0.1:6379> OBJECT encoding key
"embstr"
127.0.0.1:6379> SET key 0123456789012345678901234567890123456789
OK
127.0.0.1:6379> OBJECT encoding key
"raw"

The usage OBJECT idletime key

You can find the key is not used for a long time.

Example: Redis OBJECT idletime example

127.0.0.1:6379> SET key "PHP"
OK
127.0.0.1:6379> OBJECT idletime key
(integer) 12
127.0.0.1:6379> APPEND key Tutorial
(integer) 11
127.0.0.1:6379> OBJECT idletime key
(integer) 13
127.0.0.1:6379> GET key
"PHPTutorial"
127.0.0.1:6379> OBJECT idletime key
(integer) 15

Previous: MOVE
Next: PERSIST



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-object-key.php