PHP+redis to realize the pull model case of micro blog

  • 2021-12-13 07:42:18
  • OfStack

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

Last time, I wrote the content of a push model, but this time I shared the content of a pull model.

Pull model

Pulling the model is to show the micro-blog, get all the people who pay attention to themselves, and then pull the latest micro-blog from the people who pay attention to it.

Data structure design of micro-blog project

user Table Design

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 distinguished by userid.

Design of a single micro-blog table

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.

Design of micro-blog table for pulling followers

After each user publishes Weibo, he maintains 20 latest Weibo and saves it in an ordered collection sorted set, which is distinguished by different userid.

Note: The ordered collection of score uses Weibo id, and the collection also stores Weibo id.

Personal micro-blog form

Each user maintains his own Weibo, which is saved in the linked list, only 1000 pieces are saved, and only 1000 pieces of Weibo data are saved in redis. If you want to query more, go to the database for query.

Personal pull table design

After each user pulls the Weibo, he saves the Weibo into the pulled table, which is a linked list structure and saves up to 1000 Weibo.

Release Weibo

First, the micro-blog is saved as hash structure, and then the micro-blog is saved to the pull-out table and the personal micro-blog table.


//1 Save Weibo 
$conn = connredis();
$postid = $conn->incr('global:postid');
$conn->hmset('post:postid:'.$postid,['userid'=>$user['userid'],'username'=>$user['username'],'time'=>time(),'content'=>$content]);
//2 , everyone maintains 20 The latest Weibo is saved to an ordered collection 
$conn->zadd('starpost:userid:'.$user['userid'],$postid,$postid);
if($conn->zcard('starpost:userid:'.$user['userid']) > 20){
  $conn->zremrangebyrank('starpost:userid:'.$user['userid'],0,0);
}
//3 , maintain personal 1000 Weibo, save it to the linked list 
$conn->lpush('mypost:userid:'.$user['userid'],$postid);
if($conn->llen('mypost:userid:'.$user['userid']) > 1000){
  $conn->rpoplpush('mypost:userid:'.$user['userid'],'global:post');
}

Show Weibo

First, get all the people concerned, get the position of Weibo last time, and pull the data according to the position of Weibo last time. Then sort the microblogs, set the new pull position, write it into the pulled table, get the detailed content of microblogs, and finally get the number of fans and attention. Just show it.


//1 Gets the pull object 
$stars = $conn->smembers('following:'.$user['userid']);// People who get all the attention 
$stars[] = $user['userid'];// Need to pull your own Weibo 
//2 Gets the location of the last pull 
$lastpull = $conn->get('lastpull:userid:'.$user['userid']);
if(!$lastpull){
$lastpull = 0;
}
//3 , pull Weibo  
$latest = [];
foreach($stars as $star){
$latest = array_merge($latest,$conn->zrangebyscore('starpost:userid:'.$star,$lastpull+1,1<<32-1));
}
//4 Sort Weibo 
sort($latest,SORT_NUMERIC);
//5 Set the position of the pull 
if(!empty($latest)){
  $conn->set('lastpull:userid:'.$user['userid'],end($latest));
}
//6 Write to the pulled table 
foreach($latest as $l){
  $conn->lpush('receivepost:'.$user['userid'],$l);
}
$conn->ltrim('receivepost:'.$user['userid'],0,999);// Display at most 1000 Weibo 
//7 Get the details of Weibo 
$postids = $conn->sort('receivepost:'.$user['userid'],['sort'=>'desc']);
$posts = [];
foreach($postids as $postid){
  $posts[] = $conn->hmget('post:postid:'.$postid,['userid','username','time','content']);
}
//8 Get fans and attention 
$fansnum = $conn->scard('follower:'.$user['userid']);
$follownum = $conn->scard('following:'.$user['userid']);

Q & A

How to ensure that the pulled data is up to date?

When pulling, save the recently pulled Weibo id to redis, and then next time we only need to pull the Weibo larger than the saved Weibo id, which can ensure that the pulled data is not pulled before.

How to pull the data of all followers?

Traverse the follower and then pull the data

Suppose you pull the microblogs of A followers 1, 4, 5 B followers 2, 3, but their release times are staggered. How do you display the data?

Take out the latest tweets of all followers, and then sort them according to the tweet id.

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: