本文實(shí)例講述了PHP+redis實(shí)現(xiàn)微博的拉模型。分享給大家供大家參考,具體如下:
上回寫了一篇推模型的內(nèi)容,這回分享一篇拉模型的內(nèi)容。
拉模型
拉模型就是展示微博的時(shí)候,獲取自己的所有關(guān)注的人,然后從關(guān)注的人中拉取最新微博。
微博項(xiàng)目數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
user表設(shè)計(jì)
注冊(cè)的時(shí)候?qū)ser數(shù)據(jù)寫入redis中,key如下:
user數(shù)據(jù)的key
用戶名=user:uesrid:$uesrid:username
密碼=user:userid:$userid:password
還需要這樣寫一份,因?yàn)樾枰坑脩裘麃淼卿?,這樣就可以根據(jù)用戶名來查詢用戶id。
user:username:userid:$userid
關(guān)注的人和粉絲設(shè)計(jì)
每個(gè)用戶在產(chǎn)生關(guān)注的動(dòng)作后,在redis中維護(hù)兩個(gè)無序集合set,一個(gè)是following,一個(gè)是follower,following集合保存的是我關(guān)注的人,follower集合保存的是我的粉絲。注意是每個(gè)用戶都要維護(hù)這樣的兩個(gè)集合,用userid來區(qū)別。
單條微博表設(shè)計(jì)
每條微博的信息用hash結(jié)構(gòu)來存儲(chǔ),根據(jù)不同的微博id來區(qū)分,每條微博有如下信息:發(fā)布人id,發(fā)布人昵稱,發(fā)布時(shí)間,微博內(nèi)容。
拉取關(guān)注者微博表 設(shè)計(jì)
每個(gè)用戶發(fā)布微博后,維護(hù)20條最新微博,并保存到有序集合sorted set中,用不同的userid來區(qū)分。
注意:有序集合的score用微博id,集合保存的也是微博id。
個(gè)人微博表
每個(gè)用戶維護(hù)自己的微博,保存到鏈表中,只保存1000條,redis中只保存1000條微博數(shù)據(jù),如果想查詢更多,去數(shù)據(jù)庫中查詢。
個(gè)人已拉取表設(shè)計(jì)
每個(gè)用戶在拉取微博后,將微博保存到已經(jīng)拉取的表中,這個(gè)表是一個(gè)鏈表結(jié)構(gòu),最多保存1000條微博。
發(fā)布微博
首先將微博保存成hash結(jié)構(gòu),然后將微博保存到拉取表,還保存到個(gè)人微博表。
//1、保存微博
$conn = connredis();
$postid = $conn->incr('global:postid');
$conn->hmset('post:postid:'.$postid,['userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content]);
//2、每個(gè)人維護(hù)20條最新微博,保存到有序集合中
$conn->zadd('starpost:userid:'.$user['userid'],$postid,$postid);
if($conn->zcard('starpost:userid:'.$user['userid']) > 20){
$conn->zremrangebyrank('starpost:userid:'.$user['userid'],0,0);
}
//3、維護(hù)個(gè)人的1000條微博,保存到鏈表中
$conn->lpush('mypost:userid:'.$user['userid'],$postid);
if($conn->llen('mypost:userid:'.$user['userid']) > 1000){
$conn->rpoplpush('mypost:userid:'.$user['userid'],'global:post');
}
展示微博
首先獲取所有關(guān)注的人,獲取上次拉取微博的位置,根據(jù)上次拉取的微博位置來拉取數(shù)據(jù)。然后給微博排序,設(shè)置新的拉取的位置,寫入到已拉取表中,獲取微博的詳細(xì)內(nèi)容,最后獲取粉絲和關(guān)注數(shù)。進(jìn)行展示即可。
//1、獲取拉取對(duì)象
$stars = $conn->smembers('following:'.$user['userid']);//獲取所有關(guān)注的人
$stars[] = $user['userid'];//需要拉取自己的微博
//2、獲取上次拉取的位置
$lastpull = $conn->get('lastpull:userid:'.$user['userid']);
if(!$lastpull){
$lastpull = 0;
}
//3、拉取微博
$latest = [];
foreach($stars as $star){
$latest = array_merge($latest,$conn->zrangebyscore('starpost:userid:'.$star,$lastpull+1,132-1));
}
//4、給微博排序
sort($latest,SORT_NUMERIC);
//5、設(shè)置拉取的位置
if(!empty($latest)){
$conn->set('lastpull:userid:'.$user['userid'],end($latest));
}
//6、寫入到已拉取表中
foreach($latest as $l){
$conn->lpush('receivepost:'.$user['userid'],$l);
}
$conn->ltrim('receivepost:'.$user['userid'],0,999);//至多顯示1000條微博
//7、獲取微博的詳細(xì)內(nèi)容
$postids = $conn->sort('receivepost:'.$user['userid'],['sort'=>'desc']);
$posts = [];
foreach($postids as $postid){
$posts[] = $conn->hmget('post:postid:'.$postid,['userid','username','time','content']);
}
//8、獲取粉絲和關(guān)注數(shù)
$fansnum = $conn->scard('follower:'.$user['userid']);
$follownum = $conn->scard('following:'.$user['userid']);
QA
如何保證拉取的數(shù)據(jù)時(shí)最新的?
在拉取的時(shí)候,將最近拉取的微博id保存到redis中,然后下次我們只需要去拉取比這次保存的微博id大的微博,就可以保證拉取的數(shù)據(jù)是之前沒有拉取的。
如何拉取所有關(guān)注者的數(shù)據(jù)?
遍歷關(guān)注者,然后拉取數(shù)據(jù)
假設(shè)拉取A關(guān)注者的微博1,4,5 B關(guān)注者2,3,但是他們的發(fā)布時(shí)間交錯(cuò),怎么展示數(shù)據(jù)?
將所有關(guān)注者的最新微博都取出來,然后根據(jù)微博id進(jìn)行排序。
更多關(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ì)有所幫助。
您可能感興趣的文章:- PHP實(shí)現(xiàn)發(fā)送微博消息功能完整示例
- redis+php實(shí)現(xiàn)微博(三)微博列表功能詳解
- redis+php實(shí)現(xiàn)微博(二)發(fā)布與關(guān)注功能詳解
- redis+php實(shí)現(xiàn)微博(一)注冊(cè)與登錄功能詳解
- PHP+redis實(shí)現(xiàn)微博的推模型案例分析
- vue+php實(shí)現(xiàn)的微博留言功能示例
- php微信分享到朋友圈、QQ、朋友、微博
- PHP調(diào)用微博接口實(shí)現(xiàn)微博登錄的方法示例
- php新浪微博登錄接口用法實(shí)例
- 基于PHP實(shí)現(xiàn)發(fā)微博動(dòng)態(tài)代碼實(shí)例