php + Redis 写的相似于新浪微博的feed系统

       最近接了一个feed系统的外包,相似于微博那种!客户端是ios和android,服务器用的php,数据库用的是redis。分享下服务器和数据库部分的功能!但愿对你们有帮助。php

  关于redis的介绍,你们能够看这个百度百科 java

    首先是用户基本信息部分,包含帐号,昵称,签名,公司还有头像,咱们使用redis的hash结构(一种相似于map键值对的数据结构)结构以下:(你们在作的时候,仍是用hgetAll的命令,这样只会有一次的网络请求),注意只是基本信息,诸如玩家的粉丝,关注和帖子,咱们采起其余的数据结构体来存储!android

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,集合中的元素惟一)!ios

     收听某用户(mutli是redis提供的一种事务,这样,就能够避免产生诸如,你收听了某人,而犹豫异常,别人的粉丝中缺没有你的现象)web

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();  
}

  取消收听某用户:redis

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客户端部分的内容,咱们下篇文章见!

相关文章
相关标签/搜索