Five types of data going into detail Redis

Original article in Public number: ape Zhou Xiansen program. This platform is not updated regularly, like my article, I welcome attention to the micro-channel public number.
file

The article mentioned, Redis there are five data types most frequently used: String, List, Hash, Set , SortSet. The article simply describes the use of five data types to instructions and common scenarios, this article will talk about five kinds of data types and their respective data structures of the underlying common operation commands to parse respectively. Redis as the most popular type of Key-Value-memory database, database operations not only in memory, and can regularly be persistent data to disk, it is relatively common in many high-performance database, and in Redis, each actual Value redisObject are based on a structure represented by:
typedef struct {redisObject
unsigned type:. 4;
unsigned encoding:. 4;
void * PTR;
int the refCount;
unsigned LRU:
}
we can look at the meaning of these parameters are:

  • type: data type of the object, in general, is five data types.

  • encode: redisObject achieve the object underlying code, coding type mainly simple dynamic strings, lists, dictionaries, a jump table, a list of integers and compression.

  • * Ptr: implementation pointer points to the underlying data structure.

  • refCount: a counter, the count value is 0 when the reference objects will be released.

  • lru: last visited this object.

String data type

String data structure is a simple type of Key-Value, Redis is the most commonly used type of data, Value may be a string or a number. String data type actually stored string, an integer of three different types of floating-point values, is done automatically identify how Redis string, integer, floating-point values of three different types. Redis is implemented in C, but not using a character string in C, Redis himself actually implements a structure of type String to replace SDS:
struct sdshdr {
// record array used buf byte length
int len;
// record length of the remaining space in the array buf
int Free;
// byte array, for storing a string of
char buf [];
};

We can see that free parameters is used to determine the length of the remaining available space, len indicates the length of the string, each character buf and the end of the string stored in the '\ 0'. Why Redis To achieve their SDS structure it? Since SDS structure has several advantages:

  • 由于len保存了当前字符串的实际长度,所以获取长度时间复杂度为O(1)。
  • SDS在拼接之前会对当前字符串的空间进行自动调整和扩展,防止当前字符串数据溢出。
  • 减少内存分配次数,SDS拼接字符串发生时,如果此时的字符串长度len小于1M,则SDS会分配和len大小相同的未使用空间给free,如果此时的字符串长度len大于1M,则SDS会分配和1M的未使用空间给free,当字符串缩短时,缩短的空间会叠加到free中,用于后续的拼接使用。

String data type commonly used commands:

  • Commonly used commands: set, get, decr, incr, mget and so on.

String data type application scenarios:

  • Distributed Lock

  • Distributed session: the session is stored in the distributed application in Redis

  • Commodities spike

  • General Count: blog number, the number of reading

List Data Type
List data structure is used to store ordered plurality of strings, each string becomes List element, provides the ability to nodes List rearrangement and sequential access node, in the Redis, can ends List push and pop elements, you can also get a list of elements specified range, target acquisition and other elements at the specified index, list data structure of the main zipList (compression list) and LinkedList (doubly linked lists) implemented in two ways. First, we can look LinkedList structure:
type List struct {
// Header node
listnode head;
// table tail node
listnode
tail;
total number of nodes included //
unsigned Long len;
};

Each will be seen in the LinkedList node comprising a header head node and tail of a tail, in the LinkedList, each node has a prev points a front element, as well as a next element after a point, each node the value is the value of the node. In order to achieve a doubly linked list, in fact, to understand and C doubly linked list has a large degree of similarity. And the other is based on the continuous implementation zipList memory implementation, somewhat similar to an array of ways, but a little inconsistent and array is the size of each entry zipList may be inconsistent, require special methods to control solutions, but in the implementation of push, pop operation when there will be migrating data, the time complexity is O (n), it is generally used only when less zipList element, we can look at the structure of zipList:

struct {ziplist type
// number of bytes of compressed whole list
uint32_t zlbytes;
the number of bytes of a list of nodes to the head node // recording the compressed tail, can directly find the address of the node
uint32_t zltail_offset;
// record the number of nodes, a variety of type, default follows
uint16_t zllength;
// node
List entryX;
}

zipList in each node has the following parameter information:

  • previous_entry_length: byte length before recording node

  • content: content stored in the node, can be a byte array or an integer

  • encoding: recording data stored in the content attribute type and the length

*** List data type for the scene **

Can be used when rendering the list of articles List of data types, each user will generally have their own list of articles published, if you need to show a list of articles, you can use the List data type, and can be ordered not only to make inquiries in accordance with article index range list.

Set Data Types

Set List data types and data types is somewhat similar, can also be used to save multiple elements, but the biggest difference is that the Set point data type does not allow duplicate elements appear, and Set of elements is disordered, and we had to List Like getting elements by index index, but to support multiple types set set the set to take the intersection, union, difference, so the rational use set data type, can solve many problems in the actual project development. Set data There are two types of data structures: IntSet and HashTable. First we look at the structure of IntSet:

struct {IntSet typedef
// encoding
uint32_t enconding;
the number of elements of the set contains //
uint32_t length;
// save the array element
int8_t Contents [];
} IntSet;

When Set all elements in the collection is an integer, Redis will use IntSet data structure. It is important to pay particular attention to is: IntSet data structure are ordered. In order to reduce the consumption performance because, in the Redis Set is a set of integer elements, based on use of a dynamic array structure, while controlling the order of the elements, so that you can use a binary search algorithm when the push element to be push and pop operation elements, so that the time complexity is O (logN). When there is a non-integer data elements Set collection, Redis will then automatically use HashTable data structures to store data, only the key values ​​in the HashTable, stored value and no value, so that in HashTable, the key is always null . We can look at the structure of the HashTable:

struct {dict typedef
// Type-specific function
dictType * type;
// two hash tables, one for the real-time storage, one for the rehash
dictht HT [2];
using the data migration index // rehash
unsigned rehashidx;
}

Set data type usage scenarios:

  • Record the unique value: for example, to log ip, ID number

  • Add Tag: a tag and can cross-Set Computing degree of user preference data.

Hash data type
in Redis hash type is the key itself is a key to the structure, what we call the object, so Hash data type used to store objects is the most appropriate data type. Hash data type coding may be zipList or HashTable. When all the objects stored in the hash key for less than 64 bytes and less than 512 when the number of elements used zipList, otherwise the HashTable. List zipList the data type just mentioned zipList fact basically the same, the only difference is that increasing the number of pairs of storage Hash entry, so a certain length is an integer multiple of 2. Of course, using zipList have said push and pop time complexity of O (n), so that only in the case where only a small amount of data allowed. And in fact, somewhat similar to Java HashTable in HashTable, HashTTable relies on three main structures: dict, dictht, entry. Relationship three structures may be represented as follows this figure:
file

Hash Data Type Applicable scene:

  • Storage object data.

  • Json described in conjunction with a collection of objects.

SortSet data types

Set ordered set is based on the collection, retention characteristics can not duplicate elements Set in the collection, but the difference is, SortSet elements in the collection can be sorted, SortSet List sorting and sorting can be used as an index to sort index basis, so that SortSet achieve the orderly and key data for only two collections, SortSet data structures: zipList and skipList + HashTable, zipList do not have much, and is used when less data, the default sort an element from small to large. The use of data structures skipList + HashTable, skiplist optimum range will ensure that the time set in the case of sorted search complexity, the HashTable have already mentioned that can optimize the time complexity of the push and pop elements. skipList based on ordered list, you can create a multi-layer index, the realization of spatial complexity in exchange for practice time complexity, and ultimately the time complexity is O (logN) elements of the query process, when you need to push or pop elements, use HashTable achieve time complexity is O (1).

SortSet data type for the scene

  • Scoreboard: According Points Sort ascending

  • A range of data acquisition: Exam 80-100 data points

Welcome to public concern number: Programmer Zhou Xiansen
file

Guess you like

Origin www.cnblogs.com/niyueling/p/11586289.html