


PHP+REDIS practice: analysis of several solutions for counting online people
Online people counting business is the business logic that we must design when developing web. This article will give several design solutions to analyze the advantages and disadvantages of each solution:
Use Ordered set
This solution can store online users and user online time at the same time, and can perform a lot of aggregation calculations, but the memory consumed is also very considerable.
Use sets
This solution can store online users and can also perform certain aggregation calculations. Compared with ordered sets, it consumes less The memory is smaller, but as the number of users increases, the memory space consumed is also increasing
Use hyperloglog
This solution No matter how many online users are counted, the memory consumed is 12k, but only the statistical information of online users can be given, and the accurate list of online users cannot be obtained
Use bitmap
This solution is still relatively good. It records the status of online users while saving memory space as much as possible, and can perform certain aggregation operations.
Let’s use practical examples below. Note:
# 说 We first have a small number of users of 10 ~ 30W every day, and the user group of 100W will explain the following schemes
## 说 说 说 一 一 一 一 一 一 一 说 一 说 一 说 一 说 一 说 一 说 一 说 一 说 说 说 一 说 一 说 说 说 一 说 说 说 说 说 一 说 一 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 I. :Use an ordered set
First generate user online record data:$start_time = mktime(0, 0, 0, 9, 5); //monday for ($i=0; $i < 6; $i++) { $day_start_time = $start_time + 86400 * $i; //every day begin time $day_end_time = $day_start_time + 86400; //every day end time $online_user_num = mt_rand(100000, 300000); //online user between 100000 and 300000 for ($j=1; $j < $online_user_num; $j++) { $user_id = mt_rand(1, 1000000); $redis->zadd('000|online_users_day_'.$i, mt_rand($day_start_time, $day_end_time), $user_id); } }
//note: 统计每天的在线总人数 for ($i=0; $i < 6; $i++) { print_r($redis->zsize('000|online_users_day_'.$i). "\n"); } //note: 统计最近6天都在线的人数 var_dump($redis->zInter('000|online_users_day_both_6', [ '000|online_users_day_0', '000|online_users_day_1', '000|online_users_day_2', '000|online_users_day_3', '000|online_users_day_4', '000|online_users_day_5' ] )); //note: 统计出近6天中共有多少上线 $redis->zunion('000|online_users_day_total_6', ['000|online_users_day_0', '000|online_users_day_1', '000|online_users_day_2', '000|online_users_day_3', '000|online_users_day_4', '000|online_users_day_5']); //note: 统计某个时间段总共在线用户 print_r($redis->zcount('000|online_users_day_5', mktime(13, 0, 0, 9, 10), mktime(14, 0, 0, 9, 10))); //note: 统计某个时间段在线用户名单 print_r($redis->zrangebyscore('000|online_users_day_5', mktime(13, 0, 0, 9, 10), mktime(14, 0, 0, 9, 10), array('withscores' => TRUE)));
PHP video tutorial]
Option 2: Use collections
or First, let’s record data online for users://note set 一般聚合 for ($i=0; $i < 6; $i++) { $online_user_num = mt_rand(100000, 300000); //online user between 100000 and 300000 for ($j=1; $j < $online_user_num; $j++) { $user_id = mt_rand(1, 1000000); $redis->sadd('001|online_users_day_'.$i, $user_id); } }
//note 判断某个用户是否在线 var_dump($redis->sIsMember('001|online_users_day_5', 100030)); //note 每天在线用户总量的统计 for ($i=0; $i < 6; $i++) { print_r($redis->ssize('001|online_users_day_'.$i). "\n"); } //note 对不同时间段的在线用户名单进行聚合 print_r($redis->sInterStore('001|online_users_day_both_4and5', '001|online_users_day_4', '001|online_users_day_5'). "\n"); //note 对指定的时间段的在线用户名单进行统计 print_r($redis->sUnionStore('001|online_users_day_total_4add5', '001|online_users_day_4', '001|online_users_day_5'). "\n"); //note 哪天上线哪天没上线 print_r($redis->sDiffStore('001|online_users_day_diff_4jian5', '001|online_users_day_4', '001|online_users_day_5'). "\n");
Option 3: Use hyperloglgo
First come to the user to record data online:// note HyperLogLog 只需要知道在线总人数 for ($i=0; $i < 6; $i++) { $online_user_num = mt_rand(100000, 300000); //online user between 100000 and 300000 var_dump($online_user_num); for ($j=1; $j < $online_user_num; $j++) { $user_id = mt_rand(1, 1000000); $redis->pfadd('002|online_users_day_'.$i, [$user_id]); } }
$count = 0; for ($i=0; $i < 3; $i++) { $count += $redis->pfcount('002|online_users_day_'.$i); print_r($redis->pfcount('002|online_users_day_'.$i). "\n"); } var_dump($count); //note 3 days total online num var_dump($redis->pfmerge('002|online_users_day_both_3', ['002|online_users_day_0', '002|online_users_day_1', '002|online_users_day_2'])); var_dump($redis->pfcount('002|online_users_day_both_3'));
Option 4: Use bitmap
The author actually likes this solution. It consumes not much memory space but has a lot of statistical information. It is still old. Steps, first generate data://note bitmap 综合前面3个的优缺点 for ($i=0; $i < 6; $i++) { $online_user_num = mt_rand(100000, 300000); //online user between 100000 and 300000 for ($j=1; $j < $online_user_num; $j++) { $user_id = mt_rand(1, 1000000); $redis->setbit('003|online_users_day_'.$i, $user_id, 1); } }
//note userid today whether online var_dump($userid = mt_rand(1, 1000000)); var_dump($redis->getbit('003|online_users_day_5', $userid)); //note how many user is online var_dump($redis->bitcount('003|online_users_day_5')); //note 6 days both online var_dump($redis->bitop('AND', '003|online_users_day_both_6', '003|online_users_day_0', '003|online_users_day_1', '003|online_users_day_2', '003|online_users_day_3', '003|online_users_day_4', '003|online_users_day_5')); var_dump($redis->bitcount('003|online_users_day_both_6')); //note 6 days total online var_dump($redis->bitop('OR', '003|online_users_day_total_6', '003|online_users_day_0', '003|online_users_day_1', '003|online_users_day_2', '003|online_users_day_3', '003|online_users_day_4', '003|online_users_day_5')); var_dump($redis->bitcount('003|online_users_day_total_6')); //note 6 days only one online var_dump($redis->bitop('XOR', '003|online_users_day_only_one_6', '003|online_users_day_0', '003|online_users_day_1', '003|online_users_day_2', '003|online_users_day_3', '003|online_users_day_4', '003|online_users_day_5')); var_dump($redis->bitcount('003|online_users_day_only_one_6'));
The above is the detailed content of PHP+REDIS practice: analysis of several solutions for counting online people. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

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.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
