標籤:
最近接了一個feed系統的外包,類似於微博那種!用戶端是ios和android,伺服器用的php,資料庫用的是redis。分享下伺服器和資料庫部分的功能!希望對大家有協助。
關於redis的介紹,大家可以看這個百度百科 !
首先是使用者基本資料部分,包含帳號,暱稱,簽名,公司還有頭像,我們使用redis的hash結構(一種類似於map索引值對的資料結構)結構如下:(大家在做的時候,還是用hgetAll的命令,這樣只會有一次的網路請求),注意只是基本資料,諸如玩家的粉絲,關注和文章,我們採取其他的資料結構體來儲存!
public function updateUInfo($name,$sign,$head,$from){ $redisCli = new Redis(ServerConfig::$redisAddr); $redisCli->hSet("user:$this->uid",‘sign‘,$sign); $redisCli->hSet("user:$this->uid","name",$name); $redisCli->hSet("user:$this->uid","head",$head); $redisCli->hSet("user:$this->uid","from",$from); $redisCli->set("account:$name:uid",$this->uid); }
核心1:關注和粉絲系統!每個使用者都要維護自己的關注和粉絲系統!分別用user:$uid:followings 和 user:$uid:followers兩個欄位體現,使用的是redis的集合類型(相當於java裡面的hashSet和stl中的set,集合中的元素唯一)!
收聽某使用者(mutli是redis提供的一種事務,這樣,就可以避免產生諸如,你收聽了某人,而猶豫異常,別人的粉絲中缺沒有你的現象)
public function addFollowing($tid){ $redisCli = new Redis(ServerConfig::$redisAddr); if($redisCli->sismember("user:$this->uid:followings",$tid) == 1) { return ; } $redisCli->multi(); $redisCli->sAdd("user:$this->uid:followings",$tid); $redisCli->sAdd("user:$tid:followers",$this->uid); $redisCli->exec(); }
取消收聽某使用者:
public function removeFollowings($tid){ $redisCli = new Redis(ServerConfig::$redisAddr); if($redisCli->sismember("user:$this->uid:followings",$tid) == 0) { return ; } $redisCli->multi(); $redisCli->sRem("user:$this->uid:followings",$tid); $redisCli->sRem("user:$tid:followers",$this->uid); $redisCli->exec(); }
核心2:下面談談文章的更新:
首先是文章的基本資料結構,同上面使用者的基本資料結構,採用redis的hash結構:
class post { //put your code here private $postId = 0; private $timeStamp = 0; private $uid = 0; private $content = ""; private $subPosts = array(); private $originPostId = -1; private $tags = array(); public function __construct($id,$time,$content,$postId) { $this->uid = $id; $this->timeStamp = $time; $this->content = $content; $this->postId = $postId; } public function setOriginPostId($postId) { $this->originPostId = $postId; } public function getPostId() { return $this->postId; } public function getTimeStamp() { return $this->timeStemp; } public function getUid() { return $this->uid; } public function getContent() { return $this->content; } public function getWebTitle() { return $this->webTitle; } public function pushPostId($postId) { $this->subPosts[] = $postId; } public function saveToDb() { $redisCli = new Redis(ServerConfig::$redisAddr); $redisCli->hSet("post:$this->postId","uid",$this->uid); $redisCli->hSet("post:$this->postId","time",$this->timeStamp); $redisCli->hSet("post:$this->postId","content",$this->content); $redisCli->hSet("post:$this->postId","originPostId",$this->originPostId); $redisCli->set("post:$this->webTitle:$this->postId",$this->postId); foreach($this->tags as $tag) { $redisCli->sAdd("post:$this->postId:tags",$tag); } foreach($this->subPosts as $postId) { $redisCli->lPush("post:$this->postId:subPost",$postId); } } }View Code
每當使用者發布一個文章的時候,他的所有的粉絲必須看得到他的文章才可以!這裡我們有推和拉兩種方式,對於現在的新浪微博,據說是採取推和拉結合的兩個方式,我們的小系統。推:每當使用者發表一個新貼子的時候,就將他的文章的id主動告知他所有的粉絲。拉:使用者發布一個新的文章什麼都不要做,而當他的粉絲請求最新的內容的時候,則需要便利他所有的關注人,取得最新的文章,然後按照時間排序取出來。推比較消耗記憶體,拉則是比較消耗cpu。據說新浪微博採用推和拉結合的方式,比如當一個普通使用者則是推,但是如果是一個大明星有著幾千萬粉絲的大號,當發布一個文章的時候,則是需要他的粉絲通過拉的方式來擷取!
使用者發布一個新文章的操作:
public function post($title,$content,$tags){ $redisCli = new Redis(ServerConfig::$redisAddr); $postId = postUtil::generateNewPostId(); $redisCli->Set("user:$this->uid:postTime",time()); $redisCli->lPush("user:$this->uid:news",$post->getPostId()); $followers = $redisCli->sMembers("user:$this->uid:followers"); foreach($followers as $follower ) { $redisCli->lPush("user:$follower:news",$post->getPostId()); }}
我們將所有的文章id推到粉絲的("user:$uid:news")中,這裡採用的是順序隊列結構體!基本也就是按照時間進行了排序(最新的文章總是左邊)!我們不會將文章的內容全部到放到這個欄位裡,而是值存放了文章的id,使用者請求新鮮事的時候,自己再去拉取文章的內容!
熱門使用者/熱門文章,Redis提供了一種有序集合類型,這樣我們利用這種有序集合類型可以做熱門,熱門使用者排行和熱門文章排行!比如我們可以根據使用者的粉絲數量做排行,很容易得到前二十名熱門使用者,根據文章的閱讀量做熱門文章的排行了!
這樣一個簡單的feed系統就算是完成了!但是如果要做大,確實還是有很多系統要做!
android用戶端部分的內容,我們下篇文章見!
php + Redis 寫的類似於新浪微博的feed系統