《PHP實戰(zhàn):PHP基于Redis消息隊列實現(xiàn)發(fā)布微博的方法》要點:
本文介紹了PHP實戰(zhàn):PHP基于Redis消息隊列實現(xiàn)發(fā)布微博的方法,希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實例講述了PHP基于Redis消息隊列實現(xiàn)發(fā)布微博的方法.分享給大家供大家參考,具體如下:PHP編程
phpRedisAdmin :github地址? 圖形化管理界面PHP編程
git clone [url]https://github.com/ErikDubbelboer/phpRedisAdmin.git[/url] cd phpRedisAdmin git clone [url]https://github.com/nrk/predis.git[/url] vendor
首先安裝上述的Redis圖形化管理界面,能夠方便的管理Redis數(shù)據(jù)PHP編程
PHP編程
為了降低Mysql的并發(fā)數(shù),先把用戶的微博存在Redis中PHP編程
假設用戶發(fā)布的時候需要三個字段,uid(用戶ID號),username(用戶姓名),content('用戶的評論')PHP編程
比如用戶傳遞以下信息PHP編程
//此處需要安裝phpredis $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // 連接redis $web_info= array( 'uid' => '123456', 'username' => '123', 'content' =>'123' ); //將數(shù)組轉(zhuǎn)成json來存儲 $list = json_encode($web_info); //lpush向KEY對應的頭部添加一個字符串元素 $redis->lpush('weibo_lists',$list); $redis->close(); ///var_dump(json_encode($web_info)); var_dump($list); ?>
PHP編程
此處可以看到我們的redis已經(jīng)有數(shù)據(jù)了PHP編程
//創(chuàng)建一個PDO數(shù)據(jù)庫鏈接 data.php class qq{ public function post($uid='',$username='',$content=''){ try{ $dsn = "mysql:host;dbname=localhost;dbname=test"; $db = new PDO($dsn,'root','root'); $db->exec("SET NAMES UTF8"); $sql ="insert into test(uid,username,content)values('$uid','$username','$content')"; $db->exec($sql); }catch(PDOException $e){ $e->getMessage(); } } }
//處理redis數(shù)據(jù)庫的數(shù)據(jù) 并把數(shù)據(jù)放到MYSQL數(shù)據(jù)庫中 include "data.php"; $qq = new qq(); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); //返回的列表的大小.如果列表不存在或為空,該命令返回0.如果該鍵不是列表,該命令返回FALSE if($redis -> lsize('weibo_lists')){ //從LIST頭部刪除并返回刪除數(shù)據(jù) $info = $redis->rPop('weibo_lists'); $info = json_decode($info); $qq->post($info->uid,$info->username,$info->content); } $redis->close(); var_dump($info); ?>
PHP編程
我們能看到數(shù)據(jù)庫已經(jīng)有數(shù)據(jù)了PHP編程
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設計技巧總結(jié)》、《PHP擴展開發(fā)教程》、《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP網(wǎng)絡編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP編程
希望本文所述對大家PHP程序設計有所幫助.PHP編程
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/869.html