Home Backend Development PHP Tutorial Detailed explanation of the example code for php redis to implement instant push to 2 million users

Detailed explanation of the example code for php redis to implement instant push to 2 million users

Mar 06, 2017 am 09:23 AM

This article mainly introduces in detail how php redis can realize instant push to 2 million users. It has certain reference value. Interested friends can refer to

How to realize instant push to 2 million users. Push, this push can be understood as calling a third-party interface, push, SMS and the like.

At that time, I first wrote a demo to directly read the DB and then push the results individually. . It is conceivable

So I designed a solution based on redis+php multi-process. It is not bad in use but has high scalability, so I will share it.

====================================== ====

The specific logic is as follows: (Ignore my font)

In fact, it can be optimized here. My idea is If there is more user data, you can split the data in redis and use multiple lists. Each list corresponds to multiple php processes, which will be faster.

The following is the specific code I implemented:

Main management script: Just start this when applying.

<?php     //push推送配置 注:使用前请确认log文件为空    2016-04-12 
include_once(dirname (__FILE__)."/../../config.inc.php"); 
//if(exec(&#39;ps aux | grep redis_push.php | grep -v grep | wc -l&#39;) != 0) goto check; 
import(&#39;push.class.php&#39;); 
import(&#39;Redis.class.php&#39;); 
 
$time =time(); 
$data = array("apikey"=>&#39;xxxx&#39;,"secret"=>&#39;xxxx&#39;); 
$push = new Channel($data); 
$redis = new RedisCache($Credis[&#39;host&#39;],$Credis[&#39;port&#39;]); 
if(exec(&#39;ps aux | grep redis_push.php | grep -v grep | wc -l&#39;) != 0) goto check;//如果有推送任务 直接执行监控代码 
 
/*PUSH配置项*/ 
$config = array( 
 "file"=>"test.txt", 
 "Title"=>"sssss", 
 "Content"=>"ssssssssssssssss", 
 "OpenType"=>"0",  //1是 0否  是否跳转链接 
 "Url"=>"",     //链接地址 
 "num"=>"500",   //每次推送条数 
 "s"=>"1"      //睡眠时间 (单位:秒) 
); 
$num = 15;      //启动进程数量 
$a = $config[&#39;OpenType&#39;]==1 ? "是" : "否"; 
$c = json_encode($config); 
$info = <<<monkey 
  ************ 请确认信息是否有误*10秒后启动push任务! ************* 
  * 文件名称  : {$config[&#39;file&#39;]}; 
  * 推送标题  : {$config[&#39;Title&#39;]}; 
  * 推送内容  : {$config[&#39;Content&#39;]}; 
  * 是否跳转  : {$config[&#39;OpenType&#39;]}; 
  * 进程数量  : $num;(如果为单进程无视此项) 
  * 睡眠时间  : {$config[&#39;s&#39;]}; 
  * 日志目录  : /log; 
  ***************************************************************\n 
monkey; 
echo $info; 
sleep(3); 
$n = 1; 
while($n<=10){ 
 echo (10-$n++),"秒\n"; 
 sleep(1); 
} 
echo "------------------------- 任务已启动 -------------------------\n"; 
if($redis->Scount(&#39;push_getchannel_success&#39;)){ 
 echo "队列有未完成任务\n"; 
}else{ 
 $res = exec("php redis_getchannel.php {$config[&#39;file&#39;]}");//写入redis脚本 
 echo $res; 
} 
smtp_mail(&#39;xxxx@qq.com&#39;,&#39;推送任务已开启&#39;,&#39;请实时监测,5秒后您的手机将接收到测试推送!&#39;);//推送监控 实现定时全自动推送  
echo "\n---------------- 5秒后 test 将收到测试推送消息 ----------------\n"; 
sleep(5); 
$re = $push->BaiduPush(&#39;xxxx&#39;,&#39;xxxxx&#39;,$config[&#39;Content&#39;],$config[&#39;Title&#39;],&#39;1&#39;,$config[&#39;OpenType&#39;],$config[&#39;Url&#39;],&#39;xxxxx&#39;,$push); 
sleep(1); 
echo "\n---------------- 测试推送已发出!如未收到,请及时终止程序! 10秒后正式推送!!! ----------------\n"; 
$m = 1; 
while($m<=10){ 
 echo (10-$m++),"秒\n"; 
 sleep(1); 
} 
echo "\n---------------- 推送任务已经开始!请耐心等待! ----------------\n"; 
//下面设置是否多进程 
for($i=1;$i<=$num;$i++){ 
exec("php redis_push.php &#39;{$c}&#39; > /dev/null 2>&1 &"); 
} 
 
check: 
while(1){ 
 if(exec(&#39;ps aux | grep redis_push.php | grep -v grep | wc -l&#39;) == 0){ 
  echo "push 发送完成 用时",time()-$time,"秒"; 
  die(); 
 } 
 echo "当前进程数:",exec(&#39;ps aux | grep redis_push.php | grep -v grep | wc -l&#39;),"个","\n"; 
 echo "当前剩余推送数量:".$redis->Scount(&#39;push_getchannel_success&#39;)."\n"; 
 sleep(10); 
}
Copy after login

As for writing redis and specific push scripts, just rely on your own imagination and I won’t post them hehehehe

My approach is that the specific push script will automatically terminate and call itself after pushing a certain amount.

Because in actual applications, it is found that php scripts will freeze after running for a long time (maybe due to context switching issues), so I always avoid letting php scripts run for a long time.

Also, the users are definitely not fixed. There will be an increment of 2 million users every day. My plan is to use a scheduled script to organize the increment of users into a user table designed by myself and manage it myself. .

ps: I put all the scripts into a small PHP native framework that I organized for unified management. I will send them out after a while.

The above is the detailed explanation of the example code of PHP redis implementing instant push to 2 million users. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles