redisObject of the underlying data storage model of redis

       We know that redis is a NoSQL database that stores data in the form of key-value based on memory and supports data persistence. First of all, redis is written in C language, so we can see many files ending with .o, .c, .h in the src in the redis directory.


        What I want to introduce today is the redisObject object, which is also the object actually saved in memory after we use redis storage. I drew the following picture to explain.


Among them, we know that there are five types of data types: string, list, hash, set, and zset.

type myKey View the value type of the key myKey.

The encoding methods include raw, int, ht, zipmap, ziplist, linkedlist, intset

object encoding myKey View the encoding method corresponding to the key.

       The data pointer points to an SDS abstract object, which consists of three parts. All keys of redis are strings, so strings have special meaning in redis, and redis has a good design for the storage of strings. len in the SDS object represents the length of the char array, free represents the free length, and then includes a tail flag '\0'. In this way, we can easily know the length of the value corresponding to the key, and can perform the append operation efficiently, because the free part allocated in advance can reduce the number of memory allocations, thereby speeding up the speed of the append operation. The disadvantage is that it takes up more memory space. , and will not be actively released. The buf array is a char array, which is where the data we actually store is located. As shown in the figure above (conceptual map), hello is our data, the tail is marked with '\0', and three free areas are added.

object idletime myKey View the last time the object was accessed

object refcount myKey to see how many references the current object has (shared objects)

info View information

info memory View individual memory information

additional:

    Start the redis service: switch to the redis/src directory and execute redis-server 

    Open the client: switch to the redis/src directory and execute redis-cli

    Launch client: quit

    Shut down the redis service: enter shutdown on the client side

    Kill the redis service: find the redis service process, ps aux | grep redis

                          kill process, kill -9 pid

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325196998&siteId=291194637