PHP Redis implements real-time push instance code for 200w users

Source: Internet
Author: User
Tags php redis
This article mainly for you in detail the PHP redis implementation of 200w users of the instant push, with a certain reference value, interested in small partners can refer to

How to achieve instant push for 200w users, this push can be understood to call third-party interface, push,sms and other things.

At that time wrote a demo directly read the DB and then a single push, the result: Imagined

So the design of a set of redis+php-based multi-process solutions, with a good and go to the expansion of the high, so share.

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

The specific logic is as follows: (Ignoring my fonts)

In fact, this can also be optimized, my idea is that if the user data more, you can split the data in Redis to take a multi-list, each list corresponding to multiple PHP processes will be faster.

Here is the specific code I implemented:

Master Management script: This is enabled when applied.

<?php//push Push Configuration Note: Make sure the log file is empty 2016-04-12 include_once (dirname) before use. " /.. /.. /config.inc.php "); if (exec (' ps aux | grep redis_push.php | grep-v grep | wc-l ')! = 0) goto check; Import (' push.class.php ');  Import (' Redis.class.php '); $time =time (); $data = Array ("Apikey" = ' xxxx ', "secret" = ' xxxx '); $push = new Channel ($data); $redis = new Rediscache ($Credis [' Host '], $Credis [' Port ']; if (exec (' ps aux | grep redis_push.php | grep-v grep | wc-l ')! = 0) Goto check;//If there is a push task directly execute the monitoring code/*PUSH configuration Item */$config = Array ("File" = "Test.txt", "Title" = "sssss", "Content" = "ssssssssssssssss", "OpenType" = "0",//1 is 0 no jump Link "Url" = "" ",//LINK address" num "=" 500 ",//number of push strips" s "=" 1 "//Sleep Time (unit: seconds));      $num = 15; Number of start processes $a = $config [' OpenType ']==1? "Yes": "No"; $c = Json_encode ($config); $info = <<<monkey ************ Please confirm the information is incorrect * Start the push task in 10 seconds!   * File name: {$config [' file ']};   * Push title: {$config [' title ']}; * Push content: {$config[' Content ']};   * Whether to jump: {$config [' OpenType ']};   * Number of processes: $num;(If this is ignored for a single process) * Sleep time: {$config [' s ']};   * Log directory:/log; \ Monkey; Echo $info; Sleep (3); $n = 1;  while ($n <=10) {echo (10-$n + +), "SEC \ n"; Sleep (1); } echo "-------------------------task started-------------------------\ n"; if ($redis->scount (' push_getchannel_success ')) {echo "queue has unfinished task \ n";} else{$res = exec ("php redis_getchannel.php {$config [' file ']}");//write to the Redis script echo $res;} smtp_mail (' xxxx@qq.com ', ' push task has Open ', ' Please monitor in real time, 5 seconds after your phone will receive the test push! '); /push monitoring to achieve timed automatic push echo "\ n----------------5 seconds after test will receive testing push message----------------\ n"; Sleep (5); $re = $push->baidupush (' xxxx ', ' xxxxx ', $config [' Content '], $config [' Title '], ' 1 ', $config [' OpenType '], $config [' Url '], ' xxxxx ', $push); Sleep (1); echo "\ n----------------test push has been sent! If not received, please terminate the program in time! 10 seconds after the official push!!! ----------------\ n "; $m = 1;  while ($m <=10) {echo (10-$m + +), "SEC \ n"; Sleep (1); } echo "\ n----------------push task has started! Please be patient! ----------------\ n ";  The following settings are multi-process for ($i =1; $i <= $num; $i + +) {exec ("php redis_push.php ' {$c} ' >/dev/null 2>&1 &");} Check:while (1) {if (' ps aux | grep redis_push.php | grep-v grep | wc-l ') = = 0) {echo "Push send Complete", Time ()-$time   , "seconds";  Die ();  } echo "Current number of processes:", exec (' ps aux | grep redis_push.php | grep-v grep | wc-l '), "a", "\ n"; echo "Current remaining pushes quantity:". $redis->scount (' push_getchannel_success '). "  \ n "; Sleep (10); }

As for the write Redis and the specific push script that's all I'm going to do on my own imagination.

My practice is that a specific push script will automatically terminate and invoke itself after a certain number of pushes have been pushed.

Because the actual application found that the PHP script will be suspended after a long run (perhaps because of the context switching problem), so I have to avoid the PHP script for a long time to run.

There is a user is certainly not a fixed 200w user every day there will be an increment, my plan is to use the scheduled script every day to organize incremental users into one of my own design user table management.

PS: I put all the scripts into one of my own small PHP native framework unified management, over time I sent out.

The above is the PHP Redis implementation of 200w users of the real-time push instance code in detail, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.