A Case Study of PHP+redis Push Model for Micro blog

  • 2021-12-12 08:12:00
  • OfStack

In this paper, an example is given to describe the push model of PHP+redis to realize Weibo. Share it for your reference, as follows:

Recently, I read about redis, and then wrote a simple micro-blog project using redis. This article is about pushing models.

Push model

The so-called push model means that users will push Weibo to followers and themselves when publishing Weibo, and then other followers can see the published Weibo after logging in.

Data structure design of micro-blog project

Design of user Table

When registering, write user data to redis, and key is as follows:

key for user data
User name = user: uesrid: $uesrid: username
Password = user: userid: $userid: password

You also need to write a copy like this, because you need to log in by your user name, so you can query the user id according to your user name.


user:username:userid:$userid

Concerned people and fans design

Each user maintains two unordered sets of set in redis, one is following, the other is follower, the following set stores the people I care about, and the follower set stores my fans. Note that each user should maintain these two collections, which are partitioned by userid.

Micro-blog table design

The information of each micro-blog is stored by hash structure, which is distinguished according to different micro-blogs id. Each micro-blog has the following information: publisher id, publisher nickname, publishing time and micro-blog content.

Push table design

When users post microblogs, they push them to their fans, and each fan maintains a linked list of pushing microblogs, which only stores 50 latest microblogs. What is saved in the push table is Weibo id.

Pay attention to operation

In redis, we maintain two unordered collections, set, one is following and one is follower. The following collection holds people I care about, and the follower collection holds my fans. Note that each user maintains these two collections, which are distinguished by userid.

Core code:


$conn = connredis();// Connect redis
if($f){// If you haven't paid attention to it 
 $conn->sadd('following:'.$user['userid'],$userid);
 $conn->sadd('follower:'.$userid,$user['userid']);
}else{
 $conn->srem('following:'.$user['userid'],$userid);
 $conn->srem('follower:'.$userid,$user['userid']);
}

Release Weibo

Traverse your fans, push one Weibo to each fan, and save the pushed Weibo to each user's push table

Core code:


//1 Save Weibo 
$conn = connredis();// Connect redis
$postid = $conn->incr('global:postid');// Generate Weibo id
// Save Weibo data 
$conn->hmset('post:postid:'.$postid,['userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content]);
//2 , push Weibo 
$fans = $conn->smembers('follower:'.$user['userid']);// Get all fans 
$fans[] = $user['userid'];// You have to push it yourself 1 Portions 
foreach($fans as $u){
 $conn->lpush('receivepost:'.$u,$postid);
 $conn->ltrim('receivepost:'.$u,0,49);// Everyone saves the latest Weibo 50 Article 
}

Show Weibo

First, connect redis, then get the information of your push table, traverse the push table, then get the content of Weibo, and finally get your fans and attention. Just display the data.


// Get the pushed Weibo id
$conn = connredis();
$postids = $conn->sort('receivepost:'.$user['userid'],['sort'=>'desc']);
// Get Weibo 
$posts = [];
foreach($postids as $postid){
$posts[] = $conn->hmget('post:postid:'.$postid,['userid','username','time','content']);
}
// Get fans and attention 
$fansnum = $conn->scard('follower:'.$user['userid']);
$follownum = $conn->scard('following:'.$user['userid']);

Summarize

The micro-blog example of the push model is relatively simple to realize, but the push model has an impact on performance. If I am a big V, the number of micro-blog concerns is 2000W, and I need to push a micro-blog to 2000W when sending micro-blogs, but many people are zombie fans, so it is conceivable that there is a certain impact on performance. I can change the push model to the pull model, which can solve this problem.

For more readers interested in PHP related content, please check the topics on this site: "Summary of php+redis Database Programming Skills", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: