当前位置:网站首页>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 .
边栏推荐
- String class
- KunlunBase MeetUP 等您来!
- 2020-12-12
- Youyou1 of xlua knapsack system
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- Classes and objects
- Map的实现类的顺序性
- Flex flexible box layout
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- One dimensional array two dimensional array (sort Max insert sort)
猜你喜欢
數據庫應用技術課程設計之商城管理系統
oracle 插入单引号
IP production stream is so close to me
Haproxy+kept cluster setup 02
Unity2019_ Lighting system
Student educational administration management system of C # curriculum design
十六进制编码简介
数据分析练习题
[cocos creator] Click the button to switch the interface
Abstract classes and interfaces
随机推荐
My touch screen production "brief history" 2
Lua hot update basic grammar
Shader foundation 01
Usage of (case, when) in PostgreSQL
Golang 字符串分割,替换和截取
Basic operation and process control
【云原生】微服务之Feign的介绍与使用
One dimensional array two dimensional array (sort Max insert sort)
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
Mutual call between Lua and C #
MAE
Get to know unity2 for the first time
Xlua task list youyou
About Wireshark's unsuccessful installation of npcap
Flex flexible box layout
swagger文档配置
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
Go resolve ID card
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
What does (+) in Oracle mean