>  기사  >  백엔드 개발  >  php + Redis 写的类似于新浪微博的feed系统_PHP教程

php + Redis 写的类似于新浪微博的feed系统_PHP教程

WBOY
WBOY원래의
2016-07-13 10:05:30889검색

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

最近接了一个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);
        }
        
        
    }
    
}
 
  每当用户发布一个帖子的时候,他的所有的粉丝必须看得到他的帖子才可以!这里我们有推和拉两种方式,对于现在的新浪微博,据说是采取推和拉结合的两个方式,我们的小系统。推:每当用户发表一个新贴子的时候,就将他的帖子的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系统就算是完成了!但是如果要做大,确实还是有很多系统要做!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/963616.htmlTechArticlephp + Redis 写的类似于新浪微博的feed系统 最近接了一个feed系统的外包,类似于微博那种!客户端是ios和android,服务器用的php,数据库用的是...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.