


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!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!