Redis data structures and objects
When talking about redis data structures and objects, we have to distinguish between two concepts, one is what redis objects are, and the other is the data structures stored in redis. Redis allows memory-based interactions because of its detailed data types and cleverly assembled objects. Because many articles confuse the concept of objects with data structures, the author points out in the opening paragraph that it will be easier for you to understand what he is saying.
Objects are classified by type (commonly referred to): string objects (string), dictionary objects (hash), list objects (list), set objects (set), and ordered set objects (zset).
Underlying storage data structures such as simple dynamic strings (SDS), linked lists, dictionaries, skip lists, integer collections, and compressed lists.
This article covers the concepts of data structures and objects from a global perspective, and later articles will give a detailed description of data structures and objects respectively.
Redis object
Introduction to the
First, let’s look at what redis objects are, commonly known as string objects, dictionary objects, list objects, collection objects, and ordered collection objects. Redis distinguishes different objects according to their types, and also makes more detailed distinctions according to different object storage characteristics. For example, list objects will be stored in different data structures according to the number and type of stored elements – (linked list storage or compressed list storage). Here is the redis object structure:
structure
Typedef struct redisObject {// type - The type of the object,string, list, hash, set, zset unsigned type:4; // Encoding the underlying data structure of the object store unsigned Encoding :4; // Pointer to the underlying implementation data structure void * PTR; // counter int refcount; // Records the last time the object was accessed by a command program unsigned lru:22; }Copy the code
type
The type in the structure records the type of the object
Type constants | The name of the object |
---|---|
REDIS_STRING | String object |
REDIS_LIST | A list of objects |
REDIS_HASH | The hash object |
REDIS_SET | A collection of objects |
REDIS_ZSET | Ordered set object |
Using the TYPE command, you can view the TYPE of an object. The following is an example
redis> SET msg "hello world"
OK
redis> TYPE msg
string
Copy the code
So MSG the value of this key is a string object of type
coding
Encoding Records the encoding of the data structure of the object store. Such as double-endian lists, compressed lists, integer sets, etc., will be covered in more detail in future articles.
#define REDIS_ENCODING_RAW /* Raw representation simple dynamic string */ #define REDIS_ENCODING_INT 1 /* Encoded as integer long Integer of type */ #define REDIS_ENCODING_HT 2 /* Encoded as hash table dictionary */ #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */ #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */ #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */ #define REDIS_ENCODING_INTSET 6 */ #define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ #define REDIS_ENCODING_EMBSTR 8 /* Embedded SDS string encoding EmbstrCopy the code
You can view the value OBJECT ENCODING using the OBJECT ENCODING command, as shown in the following example
redis> SET msg "hello world"
OK
redis> OBJECT ENCODING msg
"embstr"
Copy the code
Counter refcount
Redis uses reference counting to reclaim memory, which records the number of times an object is referenced. Specific strategies are as follows
- When an object is created, refCount is initialized to 1
- When the object is referenced by a new program, the refcount is increased by one
- When the object is no longer referenced by a program, the refcount is reduced by 1
- When the object reference count is 0, the memory occupied by the object is freed at the appropriate time
Object idling duration LRU
Records the last time an object was accessed by a command program. In addition, keys with higher idle time are released first to free memory. This process is detailed in subsequent memory reclamation.
Through the above introduction, I hope to give readers a clear understanding of the object of Redis.
-
The “string data structure”, “list-type data structure” and “hash data structure” are not real data structures. They refer to redis object types.
-
Redis defines the meaning of the main parameters of the object redisObject, through these parameters to understand the ability of the object.
-
The following articles will be interspersed with an introduction to the data structures used by Redis by introducing redis objects.
The main knowledge of this paper comes from the book “Redis Design and Implementation”, but also hope that you can systematically learn knowledge through books.