国产精品成人VA在线观看,亚洲日韩在线中文字幕综合,亚洲AV电影天堂男人的天堂,久久人人爽人人爽人人av东京热

News新聞

業(yè)界新聞動(dòng)態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁      樂道系統(tǒng)FAQ      php redis實(shí)現(xiàn)對(duì)200w用戶的即時(shí)推送

php redis實(shí)現(xiàn)對(duì)200w用戶的即時(shí)推送

標(biāo)簽: 發(fā)布日期:2017-03-04 00:00:00 215
【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)

怎么實(shí)現(xiàn)對(duì)200w用戶的即時(shí)推送,這個(gè)推送可以理解為調(diào)用第三方的接口,push,sms之類的東西。

當(dāng)時(shí)先寫了一個(gè)demo 直接讀取DB然后單個(gè)推送,結(jié)果。??上攵?br />

于是設(shè)計(jì)一套基于redis+php多進(jìn)程的方案,用著還不錯(cuò)而去擴(kuò)展性蠻高的,故分享之。

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

具體的邏輯如下:(無視我的字體)

其實(shí)這里還可以優(yōu)化的,我的設(shè)想是如果用戶數(shù)據(jù)再多一些的話,可以在redis里對(duì)數(shù)據(jù)進(jìn)行分割采取多List,每一個(gè)List對(duì)應(yīng)多個(gè)php進(jìn)程這樣會(huì)更快。

下面是我實(shí)現(xiàn)的具體代碼:

主管理腳本:應(yīng)用時(shí)啟動(dòng)這個(gè)即可。 

<?php     //push推送配置 注:使用前請(qǐng)確認(rèn)log文件為空    2016-04-12 
include_once(dirname (__FILE__)."/../../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;//如果有推送任務(wù) 直接執(zhí)行監(jiān)控代碼 
 
/*PUSH配置項(xiàng)*/ 
$config = array( 
 "file"=>"test.txt", 
 "Title"=>"sssss", 
 "Content"=>"ssssssssssssssss", 
 "OpenType"=>"0",  //1是 0否  是否跳轉(zhuǎn)鏈接 
 "Url"=>"",     //鏈接地址 
 "num"=>"500",   //每次推送條數(shù) 
 "s"=>"1"      //睡眠時(shí)間 (單位:秒) 
); 
$num = 15;      //啟動(dòng)進(jìn)程數(shù)量 
$a = $config['OpenType']==1 ? "是" : "否"; 
$c = json_encode($config); 
$info = <<<monkey 
  ************ 請(qǐng)確認(rèn)信息是否有誤*10秒后啟動(dòng)push任務(wù)! ************* 
  * 文件名稱  : {$config['file']}; 
  * 推送標(biāo)題  : {$config['Title']}; 
  * 推送內(nèi)容  : {$config['Content']}; 
  * 是否跳轉(zhuǎn)  : {$config['OpenType']}; 
  * 進(jìn)程數(shù)量  : $num;(如果為單進(jìn)程無視此項(xiàng)) 
  * 睡眠時(shí)間  : {$config['s']}; 
  * 日志目錄  : /log; 
  ***************************************************************\n 
monkey; 
echo $info; 
sleep(3); 
$n = 1; 
while($n<=10){ 
 echo (10-$n++),"秒\n"; 
 sleep(1); 
} 
echo "------------------------- 任務(wù)已啟動(dòng) -------------------------\n"; 
if($redis->Scount('push_getchannel_success')){ 
 echo "隊(duì)列有未完成任務(wù)\n"; 
}else{ 
 $res = exec("php redis_getchannel.php {$config['file']}");//寫入redis腳本 
 echo $res; 
} 
smtp_mail('xxxx@qq.com','推送任務(wù)已開啟','請(qǐng)實(shí)時(shí)監(jiān)測(cè),5秒后您的手機(jī)將接收到測(cè)試推送!');//推送監(jiān)控 實(shí)現(xiàn)定時(shí)全自動(dòng)推送  
echo "\n---------------- 5秒后 test 將收到測(cè)試推送消息 ----------------\n"; 
sleep(5); 
$re = $push->BaiduPush('xxxx','xxxxx',$config['Content'],$config['Title'],'1',$config['OpenType'],$config['Url'],'xxxxx',$push); 
sleep(1); 
echo "\n---------------- 測(cè)試推送已發(fā)出!如未收到,請(qǐng)及時(shí)終止程序! 10秒后正式推送!!! ----------------\n"; 
$m = 1; 
while($m<=10){ 
 echo (10-$m++),"秒\n"; 
 sleep(1); 
} 
echo "\n---------------- 推送任務(wù)已經(jīng)開始!請(qǐng)耐心等待! ----------------\n"; 
//下面設(shè)置是否多進(jìn)程 
for($i=1;$i<=$num;$i++){ 
exec("php redis_push.php '{$c}' > /dev/null 2>&1 &"); 
} 
 
check: 
while(1){ 
 if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') == 0){ 
  echo "push 發(fā)送完成 用時(shí)",time()-$time,"秒"; 
  die(); 
 } 
 echo "當(dāng)前進(jìn)程數(shù):",exec('ps aux | grep redis_push.php | grep -v grep | wc -l'),"個(gè)","\n"; 
 echo "當(dāng)前剩余推送數(shù)量:".$redis->Scount('push_getchannel_success')."\n"; 
 sleep(10); 
}