php + Redis writes a feed system similar to Sina Weibo

Source: Internet
Author: User

recently received a feed system outsourcing, similar to the kind of microblogging! The client is iOS and Android, the server uses PHP, and the database uses Redis. Share the functions of the Server and database section! Hope to be of help to everyone.

About the introduction of Redis, we can see this Baidu encyclopedia!

The first is the user basic Information section, including accounts, nicknames, signatures, companies and avatars, we use the hash structure of redis (a data structure similar to the map key value pairs) structure as follows: (Everyone in the time, or with the Hgetall command, so there will only one network request), Note that just basic information, such as fans of players, concerns and posts, we take other data structures to store!

 Public functionUpdateuinfo ($name,$sign,$head,$from){    $redisCli=NewRedis (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: Attention and fan system! Every user has to maintain their own attention and fan system! User: $uid: Followings and User: $uid: Followers two fields, using the Redis collection type (equivalent to the hashset in Java and the set in the STL, the elements in the collection are unique)!

Listen to a user (Mutli is a transaction provided by Redis, so that you can avoid creating things like, you listen to someone, you are hesitant, others are missing you in their fans)

 Public functionAddfollowing ($tid){        $redisCli=NewRedis (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(); }

  To cancel listening to a user:

 Public functionRemovefollowings ($tid){    $redisCli=NewRedis (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: Talk about the post update:

The first is the basic data structure of the post, with the basic data structure of the above users, using the hash structure of Redis:

classPost {//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 functionSetoriginpostid ($postId)    {        $this->originpostid =$postId; }         Public functionGetpostid () {return $this-PostID; }         Public functionGettimestamp () {return $this-timestemp; }         Public functionGetuid () {return $this-uid; }         Public functiongetcontent () {return $this-content; }              Public functionGetwebtitle () {return $this-Webtitle; }         Public functionPushpostid ($postId)    {        $this->subposts[] =$postId; }         Public functionSavetodb () {$redisCli=NewRedis (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

  Every time a user posts a post, all of his fans have to see his post to get it! Here we have push and pull two ways, for now Sina Weibo, is said to be taking a push and pull combination of the two ways of our small system. Push: Every time a user posts a new post, he will actively inform all of his followers about his/her ID. Pull: The user posts a new post do nothing, and when his fans request the latest content, you need to facilitate all of his followers, get the latest posts, and then sorted by time to take out. Push compare consumes memory, pull is compare consumes CPU. Sina Weibo is said to adopt a push-pull combination, such as when a normal user is pushed, but if it is a big star with tens of millions of fans of the large, when the release of a post, it is necessary for his fans to pull through the way to get!

Action for a user to post a new post:

 Public functionPost$title,$content,$tags){    $redisCli=NewRedis (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"), which uses the sequential queue structure! Basically it's sorted by time (the newest post always left)! We will not put the content of the post into this field, but the value of the ID of the post, the user requested something new, and then to pull the content of the post!

Top users/Popular posts, Redis provides an ordered collection type so that we can use this ordered collection type to do popular, top user rankings and top posts! For example, we can be ranked according to the number of fans of the user, it is easy to get top 20 popular users, according to the amount of reading posts to do popular posts ranked!

Such a simple feed system is finished! But if you want to be bigger, there are still a lot of systems to do!

Android Client section of the content, we see the next article!

php + Redis writes a feed system similar to Sina Weibo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.