This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

11Redis Basic type

Each object in Redis is represented by a redisObject structure:

/* * Redis object */
typedef struct redisObject {

    / / type
    unsigned type:4;        

    // Do not use (alignment bit)
    unsigned notused:2;

    // Encoding mode
    unsigned encoding:4;

    // LRU time (relative to server.lruclock)
    unsigned lru:22;

    // Reference count
    int refcount;

    // Point to the object's value
    void *ptr;

} robj;
Copy the code

The three attributes associated with saving data are the Type, Encoding, and PTR attributes.

Object types are divided into the following, we can use the type command to view the object type

/* * Object type */
#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_ZSET 3
#define REDIS_HASH 4
Copy the code

The object’s PTR pointer points to the object’s underlying implementation data structures, which are determined by the object’s Encoding property.

The encoding type

/* * Object encoding * * Objects like String and Hash can have multiple internal representations. The Encoding property of the * object can be set to any of the following fields. * /
#define REDIS_ENCODING_RAW 0     /* Raw representation simple dynamic string */
#define REDIS_ENCODING_INT 1     /* Encoded as integer long */
#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  /* Encoded as intset integer set */
#define REDIS_ENCODING_SKIPLIST 7  /* Encoded as skiplist and dictionary */
#define REDIS_ENCODING_EMBSTR 8  /* Embedded SDS string encoding Embstr */
Copy the code

You can view the encoding of the value object for the database key using the object Encoding command

Using the Encoding attribute to specify the encoding used for objects, rather than having a fixed encoding associated with a particular type of object, greatly increases redis’s flexibility and efficiency in setting different encoding for objects according to different usage scenarios

Encoding of different data structures:

type coding object
REDIS_STRING REDIS_ENCODING_INT A string object implemented with an integer value
REDIS_ENCODING_EMBSTR A string object implemented using a simple dynamic string encoded by EMbstr
REDIS_ENCODING_RAW A string object implemented using a simple dynamic string
REDIS_LIST REDIS_ENCODING_ZIPLIST A list object implemented using a compressed list
REDIS_ENCODING_LINKEDLIST A list object implemented using a double-ended linked list
REDIS_HASH REDIS_ENCODING_ZIPLIST A hash object implemented using a compressed list
REDIS_ENCODING_HT Hash objects implemented using dictionaries
REDIS_SET REDIS_ENCODING_INTSET A collection object implemented using a collection of integers
REDIS_ENCODING_HT A collection object implemented using a dictionary
REDIS_ZSET REDIS_ENCODING_ZIPLIST An ordered collection object implemented using a compressed list
REDIS_ENCODING_SKIPLIST An ordered collection object implemented using skip lists and dictionaries