Redisobject of redis storage



Redis iskey-valueStorage system, wherekeyThe type is generallycharacter string, andvalueThe type is redis objectredisObject)。 Redis objects can bind various types of data, such as string, list and set. Therefore, it can well separate attributes from data.

typedef struct redisObject {
    //Just right, 32 bits
    //Object type, string / list / set / hash table
    unsigned type:4;
    //Two unused bits
    unsigned notused:2; /* Not used */
    //In order to save space, redis provides a variety of ways to save a data
    //For example: "123456789" will be stored as integer 123456789
    unsigned encoding:4;
    //Used when memory is tight and data is eliminated
    unsigned lru:22; /* lru time (relative to server.lruclock) */
    //Reference count
    int refcount;
    //Data pointer
    void *ptr;
} robj;

Among them,void *ptrExecute specific data.

Properties of redisobject data

redis.hDefined instruct redisObjectIt is a simple and excellent data structure, because in theredisObjectThe data attribute and data are separated from each other. Among them, the data attribute includes data type, storage coding mode, clock elimination and reference count.

data type

It marks what type of data the redis object is bound to. There are several possible values;

character string



define REDIS_LIST 1


define REDIS_SET 2

Ordered set

define REDIS_ZSET 3


define REDIS_HASH 4

Storage coding mode

Storage encoding, a data, can be stored in a variety of ways. For example, the data type is redis_ The data encoding mode of set may be redis_ ENCODING_ HT, or redis_ ENCODING_ INTSET。

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 */

Eliminate clock

Redis has an impact on the memory size of the datasetreal timeWhen the limit is exceeded, the overtime data will be eliminated.

Reference count

A redis object may be referenced by multiple pointers. When you need to increase or decrease references, you must call the corresponding function, and the programmer must abide by this rule. Since redis works in a single process and single thread, it is not necessary to guarantee atomicity for the operation of adding / reducing references, which is not possible in Memcache (memcached is a multi-threaded working mode, which requires mutual exclusion).


Data pointer, real data stored inptrThe address that points to.