Home > Article > Backend Development > PHP+Redis ordered collection to achieve real-time update of 24-hour rankings
Basic introduction
Redis ordered set, like a set, is also a collection of string type elements, and duplicate members are not allowed.
The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large.
The members of an ordered set are unique, but the scores can be repeated.
Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O (1). The maximum number of members in a collection is 2^32 - 1^ (4294967295, each collection can store more than 4 billion members).
An ordered set is first of all a set, and its members are unique. Secondly, each member is associated with a score, so that the members can be sorted according to the score.
Description of requirements
Imagine that there are millions of player data in a game. If you now need to compile a top 10 ranking based on the player’s experience value Bang, what would you do? The general approach is to write a SQL statement similar to the following to obtain:
select * from game_socre order by score desc limit 0,20
This method is feasible when the amount of data is small, but the query speed will be slower when the amount of data is large. , especially when joint table query is required, the speed decrease will be even more obvious.
Implementation
At this time you can consider using redis to implement this function.
The redis data type mainly used to implement this function is the redis ordered set zset. zset is an extension of the set type, which has one more sequence attribute than the original type. This attribute will automatically adjust the order value each time data is inserted to ensure that the value values are continuously arranged in a certain order.
The main implementation ideas are:
1. When a new player participates in the game, add a new record to the zset in redis (the content of the record depends on the specific requirements) score is 0
2. When the player's experience value changes, modify the player's score value
3. Use the ZREVRANGE method of redis to obtain the rankings
Return in order In the set key, the members in the specified range. The positions of the members are arranged in descending order of score value (from large to small). Members with the same score value are sorted in reverse lexicographic order. The ZREVRANGE command is identical to the ZRANGE command except that the members are arranged in order of decreasing score value.
redis 127.0.0.1:6379> ZADD KEY_NAME SCORE1 VALUE1.. SCOREN VALUEN
1. Data preparation
2. Get the score top10 ranking (ZREVRANGE is in descending order, ZRANGE is in ascending order)
3. View the actual ranking of user ee (ZREVRANK is in descending order, ZRANK is in ascending order), real-time score
Further requirements
Need to implement the latest 24-hour user points rankings and count the top 10 players and points
Implementation
Main implementation ideas Yes:
Use ZADD to add the user's points information by hour, and then use ZUNIONSTORE union to achieve the total game points for 24 hours to achieve the "24-hour ranking"; (If you have a better idea, you can It would be better if you leave a message below and give me some advice)
ZUNIONSTORE destination numkeys key [key ...]
The Redis Zunionstore command calculates the union of one or more given ordered sets, in which the number of given keys must be specified with the numkeys parameter, and the The union (result set) is stored in destination.
By default, the score of a member in the result set is the sum of the scores of that member in all given sets.
Problems you may encounter
1. Problem with the same score
When Redis encounters the same score, it follows the dictionary order of the set members themselves. Sorting, here is sorting according to the two strings "user2" and "user3". If sorted in reverse order, user3 will naturally be ranked first. To solve this problem, we can consider adding a timestamp to the score. The calculation formula is:
Score with timestamp = actual score*10000000000 (9999999999 – timestamp)
timestamp We use the system The time() function provided, that is, the number of seconds since January 1, 1970, we use a 32-bit timestamp (this can last until 2038), since a 32-bit timestamp is a 10-digit decimal integer (the maximum value is 4294967295 ), so we let the timestamp occupy the lower 10 bits (decimal integer), the actual score is expanded by 10^10 times, and then the result of adding the two parts is used as the score of zset. Considering that we are sorting in reverse chronological order, the timestamp part needs to be reversed, which is why we subtract the timestamp from 9999999999. When we want to read the player's actual score, we simply remove the last 10 digits.
Initially, this plan seems good, but there are two problems in it.
The first problem is a minor one. Using seconds as the timestamp may not be sufficiently differentiated. If two timestamps with the same score appear in the same second, the previous problem will still occur. Of course, we can choose a timestamp with higher precision, but In actual scenarios, it doesn’t matter who is in front at the same second.
The second problem is a big one, because the fraction type of Redis uses double, and the 64-bit double-precision floating point number has only 52 significant digits. The integer range it can accurately express is - 2^53 to 2 ^53, which can only represent up to 16 decimal integers (the maximum value is 9007199254740992, in fact, even 16 digits cannot be fully represented). This means that if the previous timestamp takes up 10 digits, the score will only have 6 digits, which is not enough for some leaderboard scores. We could consider reducing the number of timestamp digits, say starting on January 1, 2015, but that would still not add a few digits. Or reduce the distinction and use minutes and hours as timestamp units.
If the score type of Redis is int64, we will not have the above troubles. Speaking of which, in fact, Redis should really provide an additional int64 type ZSet, but currently it can only be a fantasy unless you change its source code.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of PHP+Redis ordered collection to achieve real-time update of 24-hour rankings. For more information, please follow other related articles on the PHP Chinese website!