当前位置:网站首页>The five basic data structures of redis are in-depth and application scenarios
The five basic data structures of redis are in-depth and application scenarios
2022-07-06 09:14:00 【~Pompeii】
Catalog
- Redis The five basic data structures are in-depth 、 Application scenarios
- 0. Business data
- 1.string
- 1.2String Basic operation
- 1.3String Application scenarios
- 1.4set And mset Choice problem
- 1.5.string matters needing attention
- 1.6string As a numerical operation
- 1.7 Application scenarios - Optimize table splitting operation
- 1.8 Application scenarios - Popular product recommendation
- 1.9 Application scenarios - Weibo fans
- 2.Hash
- 2.1Hash Introduce
- 2.2Hash Basic operation
- 2.3Hash Application scenarios
- 2.4Hash matters needing attention
- 2.5 Application scenarios - E-commerce website shopping cart design and implementation
- 2.6 Application scenarios - Rush to buy restricted goods
- 2.7 Application scenarios -string Store the object (json) And hash Store the object
- 3.List
- 4.Set
- 4.1Set Introduce
- 4.2Set Basic operation
- 4.3Set Application scenarios
- 4.4Set matters needing attention
- 4.5 Application scenarios - Hot recommendation
- 4.6 Application scenarios - Association search
- 4.7 Application scenarios - Website visit to remove duplication
- 4.9 Application scenarios - Black and white list
- 5.SortedSet
- 6.redis Business scenario - Voice call
- 7.redis Business scenario - Chat top
- 8.redis Application scenarios
Redis The five basic data structures are in-depth 、 Application scenarios
0. Business data
1.string
String The type is Redis The most commonly used type in , Internal implementation is through SDS(Simple Dynamic String ) To store the .SDS Be similar to Java Medium ArrayList, The frequent allocation of memory can be reduced by pre allocating redundant space .
Stored data : Single data , The simplest type of data storage , It is also the most commonly used data storage type
The format of data storage : One storage space holds one data
Store content : You usually use strings , If the string is presented as an integer , It can be used as a digital operation
1.2String Basic operation
1.3String Application scenarios
String The practical application scenarios of are more extensive :
1. Caching function :String String is the most commonly used data type , not only Redis, Every language is the most basic type , therefore , utilize Redis As caching , With other databases as the storage layer , utilize Redis Support for high concurrency , It can greatly speed up the reading and writing speed of the system 、 And reduce the pressure on the back-end database .
2. Counter : Many systems use Redis As the real-time counter of the system , It can quickly realize the function of counting and querying . And the final data results can be stored permanently in the database or other storage media at a specific time .
3. Share users Session: The user refreshes the interface again , You may need to access the data to log in again , Or visit the page cache Cookie, But it can be used Redis The user's Session centralized management , In this mode, we only need to guarantee Redis High availability , Every time a user Session The update and acquisition of can be completed quickly . Greatly improved efficiency .//
1.4set And mset Choice problem
By the time 3 Part of it is made up of : The client sends instructions to redisd Time for +redis Execution time +redis Time to return the result to the client ;
Let's assume The client sends instructions to redisd Time for ==redis Time to return the result to the client
1.5.string matters needing attention
1.6string As a numerical operation
1.7 Application scenarios - Optimize table splitting operation
Tip1: redis Used to control the database table primary key id, Provide generation strategy for database table primary key , Ensure the uniqueness of the primary key of the database table
This scheme is applicable to all databases , And support database cluster
1.8 Application scenarios - Popular product recommendation
Tip2:redis Control the lifecycle of data , Control business behavior through data failure , Applicable to all operations with time limited control
1.9 Application scenarios - Weibo fans
Tip3:redis It can be applied to all kinds of structured and unstructured high heat data access acceleration
2.Hash
2.1Hash Introduce
This is similar Map A structure of , In general, structured data can be used , Like an object ( The premise is that this object does not nest other objects ) To cache in Redis in , And every time I read and write the cache , It can be operated Hash A field in .
But the scene is a little more single , Because now many objects are more complex , For example, your product object may contain many attributes , There are also objects . I don't use so many scenes myself .
2.2Hash Basic operation
2.3Hash Application scenarios
This is similar Map A structure of , In general, structured data can be used , Like an object ( The premise is that this object does not nest other objects ) To cache in Redis in , And every time I read and write the cache , It can be operated Hash A field in .
2.4Hash matters needing attention
2.5 Application scenarios - E-commerce website shopping cart design and implementation
Tip4:redis Applied to the design of shopping cart data storage
2.6 Application scenarios - Rush to buy restricted goods
Tip5:redis It's applied to rush buying , Limited purchase 、 A limited number of coupons will be issued 、 Data storage design of activation code and other services
2.7 Application scenarios -string Store the object (json) And hash Store the object
3.List
3.1List Introduce
3.2List Basic operation
3.3List Application scenarios
List There is a sequence table , This one can play a lot of tricks .
For example, through List Store some list data structures , Similar to fan list 、 Comment list of articles and so on .
For example, through lrange command , Read elements in a closed interval , Can be based on List Realize paging query , This is a great feature , be based on Redis Simple high performance paging , You can do things like Weibo that pull down and page continuously , High performance , One page at a time .
For example, we can make a simple message queue , from List Head in , from List Get it out of the ass .
List It is the data structure that we often use in the development process , Hot data, let alone .
1. Message queue :Redis Link list structure of , Blocking queues can be easily implemented , You can use the command composition of left in right out to complete the design of the queue . such as : Producers of data can access Lpush Command to insert data from the left , Multiple data consumers , have access to BRpop Command blocked “ rob ” Data at the end of the list .
2. Application of article list or data page display .
such as , A list of articles from our popular blog sites , When there are more and more users , And every user has their own list of articles , And when there are many articles , All need to be displayed in pages , Consider using Redis A list of , The list is not only ordered, but also supports getting elements according to the scope , It can perfectly solve the paging query function . Greatly improve query efficiency
3.4list matters needing attention
3.5 Application scenarios - Friends circle like
Tip6:redis Applied to data control with sequence of operations
3.6 Application scenarios - Fan following list
Tip7:redis Apply to the latest news display
4.Set
4.1Set Introduce
Set Is an unordered set , It's the kind that will automatically go heavy .
Based on the direct Set Throw in the data that needs to be de duplicated in the system , It's automatic .
Can be based on Set Play together 、 Combine 、 Operation of difference set , Like intersection , We can make a whole intersection of two people's friends list , Let's see who our mutual friends are ? Right .
Anyway, there are many scenes , Because the contrast is fast , It's easy to operate , Two queries one Set Get it done .
4.2Set Basic operation
4.3Set Application scenarios
4.4Set matters needing attention
4.5 Application scenarios - Hot recommendation
Tip8:redis It is applied to random recommendation information retrieval , For example, hot song list recommendation , Hot news recommendation , Hot tourist routes , application APP recommend , Big V Recommendation, etc
4.6 Application scenarios - Association search
Tips 9:redis It is applied to the association search of similar information , Second degree association search , Deep association search
1. Show common concern ( once )
2. Show common friends ( once )
3. By the user A set out , Get friends B A list of your friends' information ( once )
4. By the user A set out , Get friends B List of shopping lists for ( Two degrees )
5. By the user A set out , Get friends B Game recharge list for ( Two degrees )
Tips 10:redis Apply to the consolidation of non duplicate data of the same type 、 Take intersection operation
4.7 Application scenarios - Website visit to remove duplication
Tip11:redis Fast de duplication for the same type of data
4.9 Application scenarios - Black and white list
Tip12:redis Apply to service settings based on blacklist and whitelist settings
5.SortedSet
5.1SortedSet Introduce
Sorted set Is the sort of Set, De duplication but sorting , Give a score when you write it in , Auto sort by score .
The usage scenario of ordered set is similar to that of set , however set Sets are not automatically ordered , and Sorted set You can use scores to sort members , And it's sorted when you insert . So when you need an ordered and non repetitive list of collections , You can choose Sorted set Data structures as options .
1. Ranking List : Orderly collection of classic use scenarios . For example, video websites need to rank videos uploaded by users , The maintenance of the list may be in many ways : According to the time 、 According to the amount of play 、 According to the number of likes, etc .
2. use Sorted Sets To make a weighted queue , For example, ordinary news score by 1, Important news score by 2, Then the worker thread can choose to press score To get the task . Give priority to important tasks .
3. Microblogging Trending , There's a heat value behind it , In front of it is the name
5.2SortedSet Basic operation
5.3SortedSet Application scenarios
5.4SortedSet matters needing attention
5.5 Application scenarios - Audition vote
Tip13:redis Applied to the ranking of the counter combination sorting function
5.6 Application scenarios - Membership expires
Tip14:redis It is applicable to scheduled task execution sequence management or task expiration management
5.7 Application scenarios - Task weight
Tip15: Apply to immediate tasks / Message queue execution management
6.redis Business scenario - Voice call
Tip16:redis Service control applied to time limited settlement by time
7.redis Business scenario - Chat top
Tips17:redis Applied to data manipulation based on time sequence , Instead of focusing on the specific time
8.redis Application scenarios
Tips 1:redis Used to control the database table primary key id, Provide generation strategy for database table primary key , Ensure the uniqueness of the primary key of the database table
Tips 2:redis Control the lifecycle of data , Control business behavior through data failure , Applicable to all operations with time limited control
Tips 3:redis It can be applied to all kinds of structured and unstructured high heat data access acceleration
Tips 4:redis Applied to the design of shopping cart data storage
Tips 5:redis It's applied to rush buying , Limited purchase 、 A limited number of coupons will be issued 、 Data storage design of activation code and other services
Tips 6:redis Applied to data control with sequence of operations
Tips 7:redis Apply to the latest news display
Tips 8:redis It is applied to random recommendation information retrieval , For example, hot song list recommendation , Hot news recommendation , Hot tourist routes , application APP recommend , Big V Recommendation, etc
Tips 9:redis It is applied to the association search of similar information , Second degree association search , Deep association search
Tips 10:redis Apply to the consolidation of non duplicate data of the same type 、 Take intersection operation
Tips 11:redis Fast de duplication for the same type of data
Tips 12:redis Apply to service control based on blacklist and whitelist settings
Tips 13:redis Applied to the ranking of the counter combination sorting function
Tips 14:redis It is applicable to scheduled task execution sequence management or task expiration management
Tips 15:redis Apply to just in time tasks / Message queue execution management
Tips 16:redis Service control applied to settlement by time
Tips 17:redis Applied to data manipulation based on time sequence , Instead of focusing on the specific time
边栏推荐
- Redis之cluster集群
- CSP salary calculation
- BMINF的後訓練量化實現
- Advanced Computer Network Review(4)——Congestion Control of MPTCP
- 不同的数据驱动代码执行相同的测试场景
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- Advanced Computer Network Review(4)——Congestion Control of MPTCP
- Advance Computer Network Review(1)——FatTree
- Selenium+pytest automated test framework practice (Part 2)
- Improved deep embedded clustering with local structure preservation (Idec)
猜你喜欢
随机推荐
Implement window blocking on QWidget
LeetCode:162. Looking for peak
QML control type: Popup
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
Multivariate cluster analysis
UML diagram memory skills
Advanced Computer Network Review(4)——Congestion Control of MPTCP
CUDA realizes focal_ loss
CUDA implementation of self defined convolution attention operator
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
[OC]-<UI入门>--常用控件的学习
Redis之主从复制
Connexion d'initialisation pour go redis
QML type: overlay
IJCAI2022论文合集(持续更新中)
[daily question] Porter (DFS / DP)
[shell script] use menu commands to build scripts for creating folders in the cluster
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
opencv+dlib实现给蒙娜丽莎“配”眼镜