当前位置:网站首页>Redis data structure
Redis data structure
2022-07-03 08:16:00 【Ma Nong Xiao Wang】
One 、redis Data structure of
1、String String type
Redis Of String Can represent string 、 Integers 、 Floating point numbers are three types of values
Application scenarios :
- Normal assignment
- Use incr、decr Command to increment and decrement statistics . Used to implement optimistic locks watch( thing )
- setNx Implement distributed locks
The underlying data type :
// data structure
struct sdshdr{
// Record buf The number of bytes used in an array
int len;
// Record buf The number of unused bytes in an array
int free;
// A character array , To hold strings
char buf[];
}
- int: When stored as int Integer of type
- embstr: Encode simple dynamic strings . The length of a single string is less than 44 Bytes
- raw: Simple dynamic strings , Big string , When the length is greater than 44 Bytes
2、List List the type
list Can be used to store ordered and repeatable data , It can quickly obtain the head and tail data , Maximum storage 2^32-1 Elements .
Underlying data structure :
1、LinkList: It's a two-way list , The structure of a two-way linked list includes a head node 、 Then each node has a head pointer to the previous element and a tail pointer to the next element .
2、zipList: Compress the list
Compressed linked list has a zitail_offset The last one was recorded entry The offset , In this way, you can quickly locate the last element of the linked list and start flashback traversal .zipList Of entry Data structure of :
typede struct entry{
// Previous entry The length of
int<var> prelen;
// Element type encoding
int<var> encoding;
// Element content
optional byte[] content;
}
Compressed linked list and common LinkList The difference between , Compressing a linked list is not about compressing elements , But the space of the linked list is allocated continuously ListList The allocated memory space is randomly allocated like this zipList Compared with LinkList There will be no fragmentation of memory . And because of zipList The space of is continuous, so it is reduced pre Pointers and next The pointer , Each pointer needs to occupy 8 Bytes , Missing two pointers reduces 16 Bytes .
because zipList Is a continuous space , And there's no pre、next Pointer so deleting a new element requires a copy of the element , And chain update operation . So when there are few elements list use zipList Storage , Use when there are many elements ListList.
3、quickList
quickList yes linkList and zipList The combination of ,linkList Every node of is a zipLit, When zipList It will be created when a certain amount of elements in linkList Of another node zipList.
Application scenarios :
- Can be used to implement stack
- Can be used to block queues
- Can be used for various lists , Such as user list 、 List of goods 、 Comment list, etc .
Reference resources :Redis Of the underlying data structure list - The world is full of dreams - Blog Garden (cnblogs.com)
3、set Collection types
set The collection type is used to store unordered and non repeating data
Underlying data structure :
1、intset: When the type of storage is integer and the number of elements is less than set-max-intset-entries When you use intset Store the data ,intset It uses an array to store data .
2、dict Dictionaries : When the stored data is not int type , Or the amount of data exceeds set-max-intset-entries Use a dictionary to store , Dictionary key Just the value of the data ,value be equal to null.
Application scenarios :
- Used to save data that cannot be repeated and does not need sorting , For example, the user information concerned
- Use spop Returns a random element in the set , And delete it 、srandmember Returning a random element in the set does not delete . These two commands are used to realize the random lottery function
4、sortset Ordered set type
Store ordered and inconsistent data .sortset All elements in will be associated with one score Score and sort by score , Scores can be repeated .
Underlying data structure :
1、ziplist: When the amount of data is small and the elements are small integers or short strings
2、skiplist: Skip list
Application scenarios :
- Realize the function of leaderboard
5、hash type
Redis hash It's a string Type of field and value Mapping table , It provides the mapping of fields and field values .
Underlying data structure :
1、ziplist: When the number of hash table elements is relatively small , And the elements are small integers or short strings .
2、dict: When the number of hash table elements is relatively large or the elements are not small integers or short strings .
Application scenarios :
- Object storage
6、bitmap type
Through one bit Bit to indicate the value or state of an element , Among them key It's the corresponding element itself .bitmap The main data is key、 Offset offset、 value , Where the value can only be 0 and 1. Greatly saved memory space
Application scenarios :
- Record employees' clock in records :key For the date , The offset is the user id, The value is 0 Indicates that you did not punch in ,1 Indicates that you have clocked in . perhaps key For the user id, The offset is date , The value is 0 Indicates that you did not punch in ,1 Indicates that you have clocked in .
边栏推荐
- A tunnel to all ports of the server
- go 解析身份证
- Mall management system of database application technology course design
- 一条通往服务器所有端口的隧道
- 【K&R】中文第二版 个人题解 Chapter1
- How to configure GDAL under idea
- UE4 plug in development
- MaxCompute字符串分割函数-SPLIT_PART
- Unity performance optimization
- Student educational administration management system of C # curriculum design
猜你喜欢
haproxy+keepalived集群搭建02
CLion-Toolchains are not configured Configure Disable profile问题解决
[untitled]
Unity4.3.1 engine source code compilation process
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
KunlunBase MeetUP 等您来!
Transplantation of freetype Library
Unity2019_ Lighting system
Base64和Base64URL
Multi traveling salesman problem -- overview of formula and solution process
随机推荐
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
the installer has encountered an unexpected error installing this package
idea取消引用顯示效果
L'installateur a été installé avec une erreur inattendue
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Unity2019_ Natural ambient light_ Sky box
regular expression
Luaframwrok handles resource updates
梯度下降法求解BP神经网络的简单Demo
Generate video using clipout in viz engine
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
String class
【K&R】中文第二版 个人题解 Chapter1
Xlua task list youyou
Wechat applet taro learning record
Golang 字符串分割,替换和截取
VMware virtual machine configuration static IP
Basic operation and process control
详解sizeof、strlen、指针和数组等组合题