编程知识 cdmana.com

Redis design and Implementation: redis object

The analysis of this paper is based on Redis 6.0 Version source code
redis 6.0 Source code :https://github.com/redis/redis/tree/6.0

stay Redis in , There are five types of data , All of them are encapsulated into one data type :redisObject. The definition is as follows :

typedef struct redisObject {
    unsigned type:4; //  type 
    unsigned encoding:4; //  code 
    unsigned lru:LRU_BITS; //  Recently visited 
    int refcount; //  Number of citations 
    void *ptr; //  Pointer to specific underlying data 
} robj;

One 、 type

typedef struct redisObject {
    unsigned type:4; //  type 
} robj;

Types are the five common object types .

The new version of the constant name is OBJ Prefix , The old version is REDIS Prefix ( Such as REDIS_STRING )

Type constant Object name
OBJ_STRING String object
OBJ_LIST List objects
OBJ_SET A collection of objects
OBJ_ZSET An orderly collection of objects
OBJ_HASH Hash object

TYPE Command can output specified key The object type of the value of .

redis > set str_key str_value
OK
redis > TYPE
string
Object name TYPE Command output
String object string
List objects list
A collection of objects set
An orderly collection of objects zset
Hash object hash

Two 、 code

typedef struct redisObject {
    unsigned encoding:4; //  code 
} robj;

Of the five types , There may be different codes for each type . The main purpose of coding is to make a balance between saving memory and improving performance .

  • When the amount of data is small , Will be used compact ( Low performance ) Data structure of
  • When the amount of data reaches a certain threshold , It's going to move from a compact structure to high efficiency Data structure of

Due to limited space , The coding is more complicated . So the coding will be written in the following article , Hey, hey, hey

3、 ... and 、 Recently visited

typedef struct redisObject {
    unsigned lru:LRU_BITS; //  Recently visited 
} robj;

When Redis When memory usage reaches the specified threshold ,Redis Memory will be recycled , There are many strategies for recycling , Among them LRU Strategy .

LRU To put it simply , Recently modified or visited , It's right in front . When it comes to elimination , Just eliminate the elements behind . The eliminated elements are relatively long without visiting / Revised .

Redis Record this latest visit time , Just for LRU The strategy is .
command OBJECT IDLETIME You can see key Since the last visit .

Due to limited space , Let's put the specific elimination strategy in the following article , Hey, hey, hey .

Four 、 Number of citations

typedef struct redisObject {
    int refcount; //  Number of citations 
} robj;

refcount The main role of the object is reference counting and memory recycling .refcount It changes with the state of use of the object :

  • When creating a new object , The value of the reference count is initialized to 1;
  • When an object is used by a new program , Its reference count will be increased by one ;
  • When an object is no longer used by a program , Its reference count is subtracted by one ;
  • When the reference count of an object changes to 0 when , The memory occupied by the object is released .

command OBJECT REFCOUNT You can view the specified key The quoted count of .

Shared objects

When will the object be used by the new program ?
To save memory ,Redis At initialization time , Create good 0 ~ 9999 The object of , follow-up 0 ~ 9999 These shared objects are used for all integer values of , Objects are not recreated .

server.h

#define OBJ_SHARED_INTEGERS 10000

object.c

// ...
if (value >= 0 && value < OBJ_SHARED_INTEGERS...)
// ...

5、 ... and 、 The pointer

typedef struct redisObject {
    void *ptr; //  Pointer to specific underlying data 
} robj;

A pointer usually points to the underlying data .
With one exception , On duty string type , And the code is int when , This is the integer value that is saved , Instead of Pointers .

About the structure of the underlying data , I'll say later .

版权声明
本文为[Xiaoxin is also]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225115815215j.html

Scroll to Top