How does Redis determine the command type? And how does Redis do memory recovery?

The commands used to operate keys in Redis can basically be divided into two types.

One of the commands can be executed on any type of key, such as DEL command, EXPIRE command, RENAME command, TYPE command, OBJECT command, etc.

For example, the following code shows the use of the DEL command to delete three different types of keys:

Insert picture description hereThe other command can only be executed on a specific type of key, for example:

  • SET, GET, APPEND, STRLEN and other commands can only be executed on string keys;
  • Commands such as HDEL, HSET, HGET, HLEN can only be executed on hash keys;
  • RPUSH, LPOP, LINSERT, LLEN and other commands can only be executed on list keys;
  • SADD, SPOP, SINTER, SCARD and other commands can only be executed on the collection key;

Commands such as ZADD, ZCARD, ZRANK, ZSCORE can only be executed on ordered set keys;

For example, we can use the SET command to create a string key, and then use the GET command and the APPEND command to operate the key, but if we try to execute the LLEN command that can only be executed by the list key on this string key, then Redis will tell us Return a type error:

Insert picture description here

How does Redis determine the command type?

As can be seen from the code example where the type error occurs, in order to ensure that only the specified type of key can execute certain specific commands, before executing a type of specific command, Redis will first check whether the type of the input key is correct, and then decide Whether to execute the given command.

The type check performed by the type-specific command is implemented through the type attribute of the redisobject structure:

  • Before executing a type-specific command, the server will first check whether the value object of the input database key is the type required to execute the command. If it is, the server will execute the specified command on the key;
  • Otherwise, the server will refuse to execute the command and return a type error to the client.

For example, for the LLEN command:

  • Before executing the LLEN command, the server will first check whether the value object of the input database key is a list type, that is, check whether the value of the type attribute of the value object redisobject structure is REDIS_LIsT, if it is, the server will execute the LLEN command on the key ;
  • Otherwise, the server will refuse to execute the command and give an example to the customer. For the LLEN command: before executing the LLEN command, the server will first check whether the value object of the input database key is a list type, that is, check the value object redisobject Whether the value of the structure type attribute is REDIS_LIsT, if it is, the server executes the LLEN command on the key;
  • Otherwise, the server refuses to execute the command and returns a type error to the client; the following figure shows this type checking process.
    Insert picture description here

How does Redis do memory reclamation?

Because the C language does not have the function of automatic memory recovery, Redis has built a memory recovery mechanism implemented by reference counting technology in its own object system . Through this mechanism, the program can track the reference counting information of the object, Automatically release objects and reclaim memory when appropriate.

The reference count information of each object is recorded by the refcount attribute of the redisobject structure:
Insert picture description here
the reference count information of the object will continue to change with the use of the object:

  • When creating a new object, the value of the reference count will be initialized to 1;
  • When the object is used by a new program, its reference count value will be increased by one;
  • When the object is no longer used by a program, its reference count value will be decremented by one;
  • When the object's reference count value becomes 0, the memory occupied by the object will be released.

The following figure lists the APIs that modify the reference count of an object. These APIs are used to increase, decrease, and reset the reference count of an object.
Insert picture description here
The entire life cycle of an object can be divided into three stages: creating an object, operating an object, and releasing an object. As an example, the following code shows the entire process of a string object from creation to release:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44533129/article/details/112676463