Home  >  Article  >  Backend Development  >  PHP + Redis writes a feed system similar to Sina Weibo_PHP Tutorial

PHP + Redis writes a feed system similar to Sina Weibo_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:30889browse

php + Redis is a feed system similar to Sina Weibo

Recently I took on the outsourcing of a feed system, similar to that of Weibo! The clients are ios and android, the server uses php, and the database uses redis. Share the functions of the server and database parts! Hope it helps everyone.
For an introduction to redis, you can read this Baidu Encyclopedia!
The first is the user’s basic information, including account number, nickname, signature, company and avatar. We use the hash structure of redis (a data structure similar to map key-value pairs). The structure is as follows: (when everyone is doing it) , still use the hgetAll command, so there will only be one network request). Note that it is only basic information, such as the player's fans, followers and posts. We use other data structures to store it!
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);
}
Core 1: Follow and fans system! Each user must maintain his or her own attention and fan system! They are represented by two fields: user:$uid:followings and user:$uid:followers respectively, using the redis collection type (equivalent to hashSet in Java and set in stl, the elements in the collection are unique)!
Listen to a user (mutli is a transaction provided by redis, so you can avoid the phenomenon that you listen to someone, but hesitate abnormally, and you are missing from other people’s fans)
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();
}
Cancel listening to a user:
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();
}
Core 2: Let’s talk about the update of the post:
First is the basic data structure of the post, which is the same as the basic data structure of the user above, using the hash structure of redis:
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());
}
}
We push all the post IDs to the fans ("user:$uid:news"), using the sequential queue structure here! Basically, they are sorted by time (the latest post is always on the left)! We will not put all the content of the post into this field, but the value will store the id of the post. When the user requests new information, he will pull the content of the post himself!
Popular users/popular posts, Redis provides an ordered collection type, so we can use this ordered collection type to do popular, popular user rankings and popular post rankings! For example, we can rank users based on their number of fans. It is easy to get the top twenty popular users and rank popular posts based on the number of posts read!
Such a simple feed system is now complete! But if you want to go big, you still need a lot of systems to do!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963616.htmlTechArticlephp + Redis is written similar to Sina Weibo's feed system. Recently, I took over the outsourcing of a feed system, similar to Weibo kind! The client is ios and android, the server uses php, and the database uses...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn