In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shares with you some tips on how to use redis to achieve rankings. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Preface
Ranking, as an almost indispensable element in Internet applications, can arouse the desire of human beings to compare themselves, so as to increase the sales of goods.
For ranking requirements, redis has a data structure that is very suitable for doing this, and that is ordered collections (sorted set).
In the development of some simple daily activities, I often encounter the need to rank users' scores, etc. At this time, I usually choose the ordered collection of redis to store users' scores, but the ways of ranking lists for different scenarios are also slightly different. The following is summarized according to my daily development.
Redis ordered set (sorted set)
First of all, let's give a brief introduction to what an ordered set is.
The Sorted Set of Redis is an ordered collection of String types. Collection members are unique, which means that there can be no duplicate data in the collection.
Each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores.
The members of an ordered set are unique, but the score can be repeated.
The collection is implemented through a hash table, so the complexity of adding, deleting, and finding is all O (1). The maximum number of members in the collection is 4294967295 (each collection can store more than 4 billion members).
Application scenario
Scenario 1: the higher the user score, the higher the ranking
This is the simplest and basic application scenario. The commands and basic operations used are as follows:
ZADD: add or update member score
Command argument: ZADD key score member [[score member] [score member].]
Add one or more member elements and their score values to the ordered set key.
If a member is already a member of the ordered set, update the score value of the member and ensure that the member is in the correct place by reinserting the member element.
The score value can be an integer value or a double-precision floating point number.
If key does not exist, create an empty ordered set and perform the ZADD operation.
An error is returned when key exists but is not an ordered set type.
Example:
/ / assuming that user A (user1)'s current game score is 50, ZADD user_rank 50 user1 / / adding user B (user2)'s current game score is 60, and user C (user3)'s current game score is 70, then you can batch operate ZADD user_rank 60 user2 70 user3 / / add the scores of user2 and user3 users at the same time, which are 2 and 3 respectively.
ZREVRANK: get the current ranking of members
Command parameter: ZREVRANK key member
Returns the ranking of member member in the ordered set key. The ordered set members are sorted by decreasing score value (from large to small).
The ranking is based on 0, that is, the member with the highest score value ranks 0.
Example:
/ / get the current ranking of user A ZREVRANK user_rank user1 / / user1 is currently ranked third, then output 2
ZSCORE: get user rankings
Command parameter: ZSCORE key member
Returns the score value of the member member in the ordered set key.
Returns nil if the member element is not a member of the ordered set key, or if key does not exist.
Example:
/ / get user A's current ranking ZSCORE user_rank user1 / / user1 current score is 50, then output "50" # Note the return value is a string
Scenario 2: the user spends the shortest time in the game, ranking higher.
This is also one of the simplest and basic application scenarios. The commands and basic operations used are similar to those in the scenario, except that the commands for obtaining rankings are not the same:
ZRANK: get the current ranking of members
Command parameter: ZRANK key member
Returns the ranking of member member in the ordered set key. The ordered set members are arranged in the order of increasing score values (from small to large).
The ranking is based on 0, that is, the member with the lowest score value ranks 0.
How to deal with the same user score in the above two scenarios
If two users have the same score, how does redis sort?
When score is the same, redis uses dictionary sorting
So what is dictionary sorting? I believe the following picture can answer this question.
When score is the same, redis uses dictionary sorting, and the so-called dictionary sorting is actually "ABCDEFG" and "123456." In this sort, if the first letter is the same, will redis compare the following letters, or sort them according to the dictionary?
Scenario 1: the higher the user score, the higher the ranking. If the score is the same, the user who achieves the score first ranks first.
In this scenario, we need to change the user's score composition as follows:
With the same score, the timestamp of the user completing the game is also added to the composition of the score value.
The user who achieves the score first ranks at the front, that is, when the score of the game is the same, the smaller the timestamp, the more front.
If we simply piece together the score structure by: score +''+ timestamp, because the larger the score is, the higher the timestamp is, and the smaller the timestamp is, the more forward it is. The judgment rules of the two parts are opposite, and it is impossible to simply combine the two into the user's score.
But we can think in reverse, we can use the same large enough number MAX minus timestamp, the smaller the timestamp, the greater the difference, so we can change the structure of score to: fraction +'+ (MAX- timestamp), so that we can meet our needs.
If you use integers as score, it is important to note that the largest integer in js is:
Math.pow (2,53)-1 / / 9007199254740991, 16 digits
The timestamp already takes up 13 digits, so there are only three digits left for us to save the user's real score.
So it's best to use the double-precision floating-point type as the score
Therefore, the best score structure is: fraction +'.'+ timestamp, which becomes a floating point number.
Scenario 2: the user who finishes the game takes the shortest time to finish the game and ranks first. If the time to finish the game is the same, the user who reaches the record first ranks first.
In this scenario, we also need to change the user's score composition as follows:
When the time to complete the game is the same, the timestamp of the user completing the game is also added to the composition of the score value.
When the game time is the same, the record user is at the front of the row first, that is, when the game score is the same, the smaller the timestamp is, the more the front row is.
The smaller the game time is, the more forward the time stamp is, so the judgment rules of the two parts are the same. We can piece together the two parts into score: score +'.'+ timestamp.
The smaller the user score, the higher the user ranking.
Thank you for reading! This is the end of this article on "how to use redis to achieve ranking tips". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.