欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP+redis實現(xiàn)微博的拉模型案例詳解

 更新時間:2019年07月10日 09:05:07   作者:webbc  
這篇文章主要介紹了PHP+redis實現(xiàn)微博的拉模型案例,結(jié)合具體實例形式詳細分析了php+redis獲取關(guān)注人最新信息的相關(guān)原理與操作技巧,需要的朋友可以參考下

本文實例講述了PHP+redis實現(xiàn)微博的拉模型。分享給大家供大家參考,具體如下:

上回寫了一篇推模型的內(nèi)容,這回分享一篇拉模型的內(nèi)容。

拉模型

拉模型就是展示微博的時候,獲取自己的所有關(guān)注的人,然后從關(guān)注的人中拉取最新微博。

微博項目數(shù)據(jù)結(jié)構(gòu)設(shè)計

user表設(shè)計

注冊的時候?qū)ser數(shù)據(jù)寫入redis中,key如下:

user數(shù)據(jù)的key
用戶名=user:uesrid:$uesrid:username
密碼=user:userid:$userid:password

還需要這樣寫一份,因為需要靠用戶名來登錄,這樣就可以根據(jù)用戶名來查詢用戶id。

user:username:userid:$userid

關(guān)注的人和粉絲設(shè)計

每個用戶在產(chǎn)生關(guān)注的動作后,在redis中維護兩個無序集合set,一個是following,一個是follower,following集合保存的是我關(guān)注的人,follower集合保存的是我的粉絲。注意是每個用戶都要維護這樣的兩個集合,用userid來區(qū)別。

單條微博表設(shè)計

每條微博的信息用hash結(jié)構(gòu)來存儲,根據(jù)不同的微博id來區(qū)分,每條微博有如下信息:發(fā)布人id,發(fā)布人昵稱,發(fā)布時間,微博內(nèi)容。

拉取關(guān)注者微博表 設(shè)計

每個用戶發(fā)布微博后,維護20條最新微博,并保存到有序集合sorted set中,用不同的userid來區(qū)分。

注意:有序集合的score用微博id,集合保存的也是微博id。

個人微博表

每個用戶維護自己的微博,保存到鏈表中,只保存1000條,redis中只保存1000條微博數(shù)據(jù),如果想查詢更多,去數(shù)據(jù)庫中查詢。

個人已拉取表設(shè)計

每個用戶在拉取微博后,將微博保存到已經(jīng)拉取的表中,這個表是一個鏈表結(jié)構(gòu),最多保存1000條微博。

發(fā)布微博

首先將微博保存成hash結(jié)構(gòu),然后將微博保存到拉取表,還保存到個人微博表。

//1、保存微博
$conn = connredis();
$postid = $conn->incr('global:postid');
$conn->hmset('post:postid:'.$postid,['userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content]);
//2、每個人維護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、維護個人的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è)置新的拉取的位置,寫入到已拉取表中,獲取微博的詳細內(nèi)容,最后獲取粉絲和關(guān)注數(shù)。進行展示即可。

//1、獲取拉取對象
$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,1<<32-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、獲取微博的詳細內(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']);

Q&A

如何保證拉取的數(shù)據(jù)時最新的?

在拉取的時候,將最近拉取的微博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ù)據(jù)?

將所有關(guān)注者的最新微博都取出來,然后根據(jù)微博id進行排序。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論