Catalog
One . sorted_set Introduction to data types
Two .sorted_set Basic operation of data type
3、 … and .sorted_set Achieve leaderboards
Four .sorted_set Type of application scenario ( One )
5、 … and .sorted_set Type application scenarios ( Two )
One . sorted_set Introduction to data types
New storage requirements : Data sorting It is conducive to the effective display of data , Need according to one can be based on Its own characteristics Of sort order
sorted_set data —- Support sorting
The required storage structure : New storage model , It can be saved Sortable The data of
sorted_set type : stay set Based on the storage structure of Sortable Field of
Two .sorted_set Basic operation of data type
1. Add data
zadd key score1 member1 score2 member2
2. Get all the data
zrange key start stop [withscores] Show permissions Forward operation From small to large
zrevrange key start stop [withscores] Show permissions The inverse From big to small
3. Delete data
zrem key memeber1 [memeber2]
4.sorted_set Common operations of
(1) Manipulate data conditionally
zrangebyscore key min max [withscore] [limit] from score From one range to another positive sequence
zrevrangebyscore key max min [withscore] [limit] from score From one range to another flashback
(2) Delete data conditionally
zremrangebyrank key start stop Delete by index
zremrangebyscore key min max Delete by score authority
Be careful :
min And max be used for Limited search query Conditions
start And stop Used to limit the scope of a query , On the index , Express The index of the beginning and the end
offset and count Used to limit the scope of a query , Apply to query results , Express Starting position and total amount of data
5. Get total collection data
zcard key
zcount key min max see score How many data are in the data range
6. Set intersection and operation
zinterstore destination numkeys key [key …] intersection
zunionstore destination numkeys key1 [key …] Combine
Meeting and meeting will score Sum the weights
3、 … and .sorted_set Achieve leaderboards
Sort The problem of
Business scenario :
For all ranking Set up the ranking of resources based on
Solution :
Get the index of the data ( ranking )
zrank key member
zrevrank key member
score Value acquisition and modification
zscore key member
zincrby key increment member
tips13:
redis be applied to Counter Combine Sort Function corresponding to ranking
sorted_set type
1.score The data storage space saved is 64 position , The integer range is -9007199254740992~9007199254740992
2.score The saved data can also be a Double precision doubbo value , be based on Characteristics of double precision floating point numbers , But you may lose precision
3.sorted_set The underlying storage is still be based on set Structural , therefore Data cannot be repeated , If you add the same data repeatedly ,score Values will be repeatedly overridden , Keep the result of the last modification
Four .sorted_set Type of application scenario ( One )
Business scenario
1.vip Experience , The website opens regularly, and the voting discussion is limited sorted_set Effectively manage this kind of information
Solution
1. For timeline based task processing , Record the processing time as score value , Use the sorting function to distinguish the order before and after processing
2. Record the next time to process , When it’s due, process the corresponding task , remove redis Records in , And record the next time to deal with it
3. When a new task is added , Determine and update the current next task time to be processed
4. In order to improve sorted_set performance , Usually, tasks are stored into several sorted_set, For example 1 Within hours ,1 Days. ,1 bring a false charge against an innocent person , In a month , quarter , Year etc , Several tasks of operation are incorporated into 1 In the queue processed within hours
tips14:
redis be applied to timing Mission Execution order Management and tasks Expiration Management
5、 … and .sorted_set Type application scenarios ( Two )
Business scenario
Mission / Message weight Set the app
When a task or message is pending , When forming a task queue or message queue , For high priority tasks, we should guarantee the priority application of them , How to realize task weight management , Delay queue
Solution :
For weighted tasks , Give priority to high priority tasks , take score Record weight that will do
Multi condition task weight setting
If there are too many weight conditions , You need to sort score value , To deal with , guarantee score Values are compatible with 2 Conditions or conditions , for example
1. Foreign trade orders are better than domestic orders , President orders are better than employee orders
2. because score Limited length , Data needs to be truncated , In particular, the time can be set in hours or minutes ( After the discount )
3. First set the order class to be , After setting the order initiation role category , Whole score The length must be uniform , Fill in the deficiency 0, The first rank of the first collation must not be 0
for example foreign trade 101 At home 102 The manager 004 staff 008
The foreign trade order issued by the employee is 101008( Small )
The manager’s domestic order is 102004( Big )
tips15:
redis application Timing task / Message queue Time management