Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the five data types of Redis?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "What are the five major data types of Redis?" Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "What are the five major data types of Redis?"

As mentioned in the previous article, there are five most frequently used data types in Redis: String, List, Hash, Set, SortSet. The previous article simply introduced the instructions and common scenarios used by these five data types. This article will talk about the underlying data structures of the five data types and their common operation commands to analyze them separately. Redis is currently the most popular Key-Value in-memory database. Not only are database operations performed in memory, but data can be persisted to disk regularly, so the performance is much higher than that of ordinary databases. In Redis, each Value is actually represented by a redisObject structure: typedef struct redisObject{ unsigned type:4; unsigned encoding:4; void *ptr; int refCount; unsigned lru: } We can look at the meaning of these parameters:

type: The data type of the object, generally the five major data types.

encode: redisObject object underlying encoding implementation, the main encoding types are simple dynamic string, linked list, dictionary, jump table, integer collection and compressed list.

ptr: Pointer to the underlying implementation data structure.

refCount: Counter that releases objects when the reference count is 0.

lru: The last time this object was accessed.

String data type

String data structure is a simple Key-Value type, which is one of the most commonly used data types in Redis. Value can be string or number. String data type can actually store string, integer, floating point three different types of values, Redis is how to automatically recognize string, integer, floating point three different types of values. Redis is implemented in C, but does not use strings in C. In fact, Redis implements a structure SDS instead of String type: struct sdshdr{ //record the length of used bytes in buf array int len; //record the length of remaining space in buf array int free; //byte array, used to store string char buf[]; };

We can see that the free parameter is used to determine the length of the remaining usable space, len indicates the length of the string, buf stores each character of the string and the '\0'at the end. Why does Redis implement SDS structs on its own? Because SDS structures have several advantages:

Since len stores the actual length of the current string, getting the length takes O(1) time.

SDS automatically adjusts and expands the space of the current string before concatenation to prevent the current string data from overflowing.

When SDS concatenates strings, if the string length len is less than 1M, SDS allocates unused space of the same size as len to free; if the string length len is greater than 1M, SDS allocates unused space of and 1M to free; when the string is shortened, the shortened space will be superimposed on free for subsequent concatenation.

String Data Type Common commands:

Common commands: set, get, decr, incr, mget, etc.

String Data Type Applicable Scenarios:

distributed lock

Distributed session: Store distributed application sessions in Redis

Product second kill

General count: number of blogs, number of reads

List data type List data structure is used to store multiple ordered strings, each string in the List becomes an element, List provides the ability to rearrange nodes and access nodes sequentially, in Redis, List can push and pop elements at both ends, you can also obtain a list of elements in a specified range, obtain elements with specified index subscripts, etc. List data structure mainly has zipList(compressed linked list) and LinkedList(doubly linked list) two implementations. First, we can look at the structure of LinkedList: type struct list{ //listNode *head; //listNode *tail; //unsigned long len; };

You can see that each LinkedList contains a head node and a tail node. In LinkedList, each node has a prev pointing to the previous element and a next pointing to the next element. The value of each node is the value of the node. Thus achieve a double linked list, understand up in fact and C in the double linked list has a large degree of similarity. Another implementation is zipList, which is based on continuous memory implementation. It is somewhat similar to array mode, but it is a bit inconsistent with array. The size of each entry of zipList may not be consistent, and special methods are needed to control the solution. However, when performing push, pop operations, there will be data migration. The time complexity is O(n), so zipList is generally used only when there are few elements. We can look at the structure of zipList:

type struct ziplist{ //The number of bytes in the compressed list uint32_t zlbytes; //Record the number of bytes from the tail node to the head node of the compressed list. You can directly find the address of the node uint32_t zltail_offset; //Record the number of nodes. There are many types. The default is uint16_t zllength; //Node List entryX; }

Each node in zipList has the following parameter information:

previous_entry_length: Record the byte length of the previous node

content: The content stored in the node, which can be an array of bytes or an integer

encoding: records the type and length of data stored in the content attribute

*** List Data Type Applicable Scenario **

List data type can be used when rendering the list of articles. In general, each user will have his own published list of articles. If you need to display the list of articles, you can use List data type. You can query the list of articles not only in order but also according to the index range.

Set data type

Set data type is similar to List data type, it can also be used to store multiple elements, but the biggest difference is that Set data type does not allow duplicate elements, and the elements in Set are out of order, so there is no way to obtain elements through index subscript like List, but Set type supports intersection, union and difference of multiple Set sets, so reasonable use of Set data type can solve many problems in actual project development. The Set data type has two data structures: IntSet and HashTable. First let's look at the structure of IntSet:

typedef struct intset { //encoding uint32_t enconding; //number of elements in the collection uint32_t length; //array int8_t contents[]; } intset;

Redis uses the IntSet data structure only when all elements in the Set collection are integers. One thing to note is that the IntSet data structure is ordered. Because in order to reduce performance consumption, Redis uses a dynamic array-based structure when the elements of the Set are integers, and controls the size order of the elements when pushing them, so that binary search algorithms can be used to push and pop elements, so that the time complexity is only O(logN). When there is non-integer data in the elements of the Set collection, Redis will automatically use the HashTable data structure to store the data. In the HashTable, only the key value and no value are stored, so in the HashTable, the key value is always null. We can look at the structure of HashTable:

typedef struct dict{ //type-specific function dictType *type; //hash tables Two, one for real-time storage and one for rehash dictht ht[2]; //rehash index Use unsigned rehashidx when migrating data; }

Set Data Type Usage Scenarios:

Record unique values: such as login ip, ID number

Add tags: You can calculate user preferences and other data through the intersection and union of tags.

Hash data type In Redis, hash type refers to the key itself and a key-value pair structure, which is what we call an object, so Hash data type is the most appropriate data type for storing objects. The encoding of the Hash data type can be zipList or HashTable. Use zipList when the hash object holds all key-value pairs less than 64 bytes in length and fewer than 512 elements, otherwise use HashTable. zipList is basically the same as zipList mentioned in List data type, the only difference is that the number of Hash entries increases in pairs, so the length must be an integer multiple of 2. Of course, using zipList has just said that push and pop time complexity is O(n), so only in the case of small data is allowed to use. HashTable is similar to HashTable in Java. HashTTable mainly depends on three structures: dict, dictht and entry. The relationship between the three structures can be expressed as follows:

Hash data type Applicable scenarios:

Store object data.

Json describes the collection of objects.

SortSet data type

SortSet is based on Set collection, retaining the feature that there can be no duplicate elements in Set collection, but the difference is that elements in SortSet collection can be sorted, SortSet sorting and List sorting can use index subscripts as sorting basis, so SortSet realizes data ordering and key value pairs unique collection, SortSet data structure has two kinds: zipList and skipList + HashTable, zipList is not much, is used for small data volume cases, the default sorting is elements from small to large. With the skipList + HashTable data structure, skipList optimizes the time complexity of range lookups while keeping the collection in order, while HashTable has just mentioned that it optimizes the time complexity of push and pop elements. skipList is based on ordered lists, and can create multi-level indexes to achieve space complexity in exchange for time complexity. Finally, it realizes the element query process with time complexity of O(logN). When push or pop elements are needed, HashTable is used to achieve time complexity of O(1).

SortSet data type Applicable scenarios

Points ranking: sorted according to points from small to large

Get a range of data: 80-100 points on the exam

At this point, I believe that everyone has a deeper understanding of "what are the five major data types of Redis?" Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report