data storage structure redis

1.redis to the stored key-value, key string is fixed, string objects represented, value can be a string (String), a list (List), Hash (the Hash), set (the Set), ordered set (zSet).

2. In the memory, each of the Key-Value is divided into DictEntry, RedisObject and specific objects, DictEntry and each comprising a pointer pointing to Key and Value, and the pointer points to the next DicEntry, key value and a pointer is to RedisObject form

3.redis using custom type string, the string is different from the C language, using Redis sdshdr structure to represent a string object (SDS)

struct sdshdr {

  int len;

  int free;

  char buf[];

}

len attributes: the length of the string

free attributes: Number of unused bytes

buf array: the realization of the underlying string for character

buf array will \ 0 null characters, the null character is not recorded in the len property

Length array of bytes stored in the C language string is its total length N + 1, because the last one at the end, so if added to the string, reallocate memory, Redis of unused space released by SDS the relationship between the length and the string length of the bottom of the array, the length of buf is not necessarily the SDS string length + 1, which further may comprise an array of unused bytes, by which not only the space, on SDS to achieve a pre-allocated space and space for the release of two strategies inert, reducing the number of re-allocation of memory is caused due to the string modification.

Pre-allocated space is used to optimize the growth of operating a string of SDS saved, when you need to save a string SDS grow operations, in addition to the space program will be allocated, but also to allocate additional unused space SDS

Inert saved for optimizing the space freed SDS shortened string operation, when it is necessary to shorten the string SDS save operation, the program will not be immediately recovered byte memory reallocation of the extra shortened, but the use of free property these extra number of bytes recorded for possible future use.

 

Guess you like

Origin www.cnblogs.com/wangflower/p/12233858.html