redis+php實(shí)現(xiàn)微博(二)發(fā)布與關(guān)注功能詳解
本文實(shí)例講述了redis+php實(shí)現(xiàn)微博發(fā)布與關(guān)注功能。分享給大家供大家參考,具體如下:
數(shù)據(jù)結(jié)構(gòu):
set post:postid:3:time timestamp
set post:postid:3:userid 5
set post:postid:3:content 測(cè)試發(fā)布哈哈哈哈incr global:postid
set post:postid:$postidcho "用戶名密碼不能夠?yàn)榭?";
關(guān)注微博
following:3
被關(guān)注(粉絲)
followed:3
把發(fā)布的微博推給自己的粉絲
recivepost:10 postid
微博的發(fā)布代碼:
include("function.php"); include("header.php"); $content = I('content'); if(!$content){ error('內(nèi)容不能夠?yàn)榭?); } $user = isLogin(); if($user==false){ header("location:index.php"); exit(); } $r = redis_connect(); $postid = $r->incr('global:postid'); //$r->set("post:postid:".$postid.":time",time()); //$r->set("post:postid:".$postid.":userid",$user['userid']); //$r->set("post:postid:".$postid.":content",$content); $r->hmset("post:postid:".$postid,array('userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content)); //把微博推給自己的粉絲 $fans = $r->smembers("followed:".$user['userid']); $fans[] = $user['userid']; foreach($fans as $fansid){ $r->lpush('recivepost:'.$fansid,$postid); } //單獨(dú)累計(jì)個(gè)人發(fā)布的信息 $r->lpush('userpostid:'.$user['userid'],$postid); header("location:home.php"); exit; include("bottom.php");
微博的關(guān)注代碼:
include("function.php"); include("header.php"); if(isLogin()==false){ header("location:index.php"); exit; } $user = isLogin(); $uid = trim($_GET['uid']); $f = trim($_GET['f']); $r = redis_connect(); if($f==0){ //將關(guān)注與被關(guān)注的數(shù)據(jù)結(jié)構(gòu)存入redis $r->sadd("following:".$user['userid'],$uid); $r->sadd("followed:".$uid,$user['userid']); }else{ //取消關(guān)注 $r->srem("following:".$user['userid'],$uid); $r->srem("followed:".$uid,$user['userid']); } //根據(jù)傳遞過來的userid查找username $uname = $r->get("user:userid:".$uid.":username"); header("location:profile.php?u=".$uname); include("bottom.php");
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP正則的Unknown Modifier錯(cuò)誤解決方法
PHP正則時(shí)出現(xiàn)Unknown Modifier錯(cuò)誤解決方法2010-03-03php實(shí)現(xiàn)點(diǎn)擊可刷新驗(yàn)證碼
這篇文章主要介紹了php如何實(shí)現(xiàn)點(diǎn)擊即可刷新驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11中國(guó)站長(zhǎng)站 For Dede4.0 采集規(guī)則
中國(guó)站長(zhǎng)站 For Dede4.0 采集規(guī)則...2007-05-05php編程實(shí)現(xiàn)獲取excel文檔內(nèi)容的代碼實(shí)例
最近剛剛做了個(gè)導(dǎo)入excle文件,群發(fā)短信的功能,在這里總結(jié)下用php獲取excle文件數(shù)據(jù)的方法2011-06-06php使用str_replace替換多維數(shù)組的實(shí)現(xiàn)方法分析
這篇文章主要介紹了php使用str_replace替換多維數(shù)組的實(shí)現(xiàn)方法,結(jié)合具體實(shí)例對(duì)比分析了php針對(duì)多維數(shù)組的遍歷與替換操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-06-06