redis object type

 

   redis object type

      Redis does not use the main data structures we mentioned earlier: SDS, linked list, dictionary, compressed list, jump list as a direct implementation of redis objects. Instead, an object system is created based on these data structures.

      This system includes string objects, hash objects, list objects, collection objects, and ordered collection objects. Each object uses at least one of the data structures we introduced earlier.

1. Object type and encoding

     Redis uses objects to represent keys and values ​​in the database. Every time a new key-value pair is created, at least two objects are created, a key object and a value object. For example: set msg "hello". msg is the key object, hello is the value object.

    Each redis object is represented by a redisObject.

    

typedef struct redisObject {
    unsigned type:4;//Type
    unsigned encoding: 4;//Encoding
    unsigned lru:REDIS_LRU_BITS;
    int refcount;/*reference count*/
    void * ptr;
} robj;

 

     1.1 Types, Encoding Formats and Their Correspondence

    The types of objects correspond to string objects, hash objects, list objects, collection objects and ordered collection objects respectively.

#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_ZSET 3
#define REDIS_HASH 4

   The encoding of objects corresponds to the eight basic types introduced earlier

#define REDIS_ENCODING_RAW 0     /* Raw representation */
#define REDIS_ENCODING_INT 1     /* Encoded as integer */
#define REDIS_ENCODING_HT 2      /* Encoded as hash table */
#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  /* Encoded as intset */
#define REDIS_ENCODING_SKIPLIST 7  /* Encoded as skiplist */

   The storage structure of each type is shown in the following figure:

write picture description here

 

 

Guess you like

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