In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to use Redis to achieve rankings. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1. Preface
To achieve a ranking, what we usually think of is that mysql's order by is simply rudely pulled out. But is this really elegant?
It is well known that the database is the bottleneck of the system. If you were given a multimillion-dollar table and asked to sort the rankings, it would take a terrible time.
How about caching? indexes are forced to be used in order by. But is this really elegant?
We analyze the ranking, one ranking for each user, which means to repeat, then we will think of Set, a data structure of Java. But Set is disordered. Is there a structure that can keep the elements unique and orderly?
Fortunately, there is. Redis's ZSet is such a data structure. The elements in Zset are unique and ordered, sorted by score from smallest to largest. As a good crud programmer, we understand the structure of zset from these aspects.
2.1.Adding and modifying ZADD
Its time complexity is O (M*log (N)), where N is the cardinality of the ordered set and M is the number of new members successfully added. Insert if key does not exist and update if it does.
The use is as follows:
Description:
Page_rankde is key,10 is a score, google.com is value.
2.2, ZRANK query
Time complexity: O (log (N))
The use is as follows:
Description:
The key,tom of salary is value, and you can query the corresponding ranking as long as you enter a specific key and value.
Del deletion
Use redis's del command directly
Going back to the implementation of the ranking, if you want to use the zset structure to achieve it, the important thing is how to design the score. Analyze the design of the ranking list. If the ranking is designed according to a dimension such as the number of gold coins, it is only necessary to reverse the number as a score score. The reverse is because zset is sorted from smallest to largest by default.
The implementation is as follows:
If the ranking is designed in two dimensions, such as the number of gold coins and time. Because score is a parameter that can be of type double, you can design it as a decimal, using the total milliseconds of a day minus milliseconds as a decimal part, then concatenating it as a string, and then inverting it as a score.
The implementation is as follows:
Code implementation
After reading the above, do you have any further understanding of how to use Redis to achieve ranking? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.