Analysis of Weibo's attention relationship with RedisFocus on the four relationship states that the relationship produces
- Concern
- Fans
- Bi-directional attention (mutual powder)
- No relationship
Demand analysis
On Weibo, each user will have a watchlist, a list of followers. Users can view their concerns, fan list, or view other people's concerns, fan lists. And, to show the attention of everyone in the list and the current viewer. The likelihood of a state is the four relationship States mentioned above.
The problem can be seen in two different situations:
- Watch your own attention, fan list
- Watch other people's attention, fan list
Watch your own attention, fan list:
This is a relatively simple situation. For example, to look at their own watchlist, the list of people's relationship with their own state can not be "no relationship" and "fan." It can only be "attention" and "two-way attention". Similarly, the list of fans has only two states.
Watch other people's attention, fan list:
This is the most complicated situation, if you look at other people's attention lists, the list of people and themselves may have all of the above four relationship status.
Analyze from a collection of graphs
As shown in. The circle on the left represents the user's watchlist, the circle on the right represents the fan list, and the circle below represents the list (collection) to view. Use follow, fans, find to indicate these three sets.
When looking at your own list, it actually means that the Find collection is a subset of one of the above collections. For example, look at your fans and say that find is a subset of fans and look at your concerns, indicating that find is a subset of follow.
When you view a list of people, the intersection of three sets is generated in the diagram. Users who want to query the collection may be in your fans, in the collection of concerns, or not. That is, it could be any relationship state, and the root of the problem is that we want to figure out the state of each user's relationship with the current user. To solve the four relationship states, we must ask for three small intersections in the lower part of the graph.
- The collection to query with my mutual powder intersection
- The collection to query is the intersection of my concerns
- The set of queries to query with my fan intersection
Users who are not in these three small-handed sets are the stateless users.
If we use the following set of names:
Focus on the collection
Follow:userid Fan Collection Fans:userid
Mutual Powder Collection (temporary)
Fofa:userid the collection to query (temporary) Find:userid
The collection to query intersects with my concerns (temporary)
Find_inter_follow:userid the set to query with my fans intersection (temporary) Find_inter_fans:userid
The collection to query with my mutual powder intersection (temporary)
Find_inter_fofa:userid
Other in find is not concerned
Using sorted set storage relationships
Score is used to store the time of interest, and each user stores two collections. Follow:userid stores users ' concerns, Fans:userid stores users ' fans. So we can design a function to find out the set of these states.
function returns:
"findSet" => $findSet, //要查询的集合"fofaSet" => $fofaSet, //互粉的集合"findInterFollowSet" => $findInterFollowSet, //要查询的集合与我的关注交"findInterFansSet" => $findInterFansSet //要查询的集的与我的粉丝交
To find out the above four sets, you can conduct relationship state judgment, first of all to determine whether the mutual powder, if not mutual powder, and then determine whether it is my concern, if not, then judge whether it is my fans. If it's not just a matter of nothing. So we can get the status out.
/** UserID: Current user id* Targetuserid: The id* of the person being viewed Findtype: Which list is viewed * Findstart: The location where the list of paginated views starts * Findend: the location where the list of paginated views ends */function Getchunksets ($redis, $userID, $targetUserID, $findType, $findStart, $findEnd) {$fansKey = "fans:". $userID; $followKey = "Follow:". $userID; $findKey = "Find:". $userID; $targetKey = $findType. ":" . $targetUserID; $fofaKey = "Find_inter_fofa:". $userID; $findInterFollowKey = "Find_inter_follow:". $userID; $findInterFansKey = "Find_inter_fans:". $userID;//Find the collection element to query $findset = $redis->zrevrange ($targetKey, $findStart, $findEnd, TRUE);//To query the collection with my attention pay $ Findinterfollowset = Array ();//The set to query with my fan $findinterfansset = array ();//First clear the temporary set $redis->del ($findKey); $redis- >del ($fofaKey), $redis->del ($findInterFollowKey), $redis->del ($findInterFansKey);//Save up foreach ($findSet As $uid = $score) {$redis->zadd ($findKey, $score, $uid);} Find the mutual powder set if ($userID! = $targetUserID) {//See others $redis->zinter ($fofaKey, Array ($findKey, $fansKey, $followKey));/* * If you are not looking at your own list, also ask for * 1: toA collection of queries with my followers * 2: To query the set of my fans/$redis->zinter ($findInterFollowKey, Array ($findKey, $followKey)); $redis Zinter ($findInterFansKey, Array ($findKey, $fansKey)); $findInterFollowSet = $redis->zrevrange ($ Findinterfollowkey, 0,-1); $findInterFansSet = $redis->zrevrange ($findInterFansKey, 0,-1);} else {if ($findType = = "fans") {//Watch your fan list $redis->zinter ($fofaKey, Array ($findKey, $followKey));} else if ($findType = = "Follow") {//Watch your watchlist $redis->zinter ($fofaKey, Array ($findKey, $fansKey));}} Mutual powder Set $fofaset = $redis->zrevrange ($fofaKey, 0,-1); return Array ("findset" = $findSet,//set to query "Fofaset" + = $ Fofaset,//Powder Collection "Findinterfollowset" + $findInterFollowSet,//To query the collection with my followers "findinterfansset" = $ Findinterfansset//To query the set of with my fans);}
The above function has already obtained the required set, then the relationship state is judged.
/** isSelf: 是否查看自己的列表* findType: 查看的是粉丝还是关注列表 1: 关注, 2: 粉丝* userInfoArr: 用户详细信息数组*/function getUserInfoList($isSelf, $findType, $userInfoArr, $findSet, $fofaSet, $interFansSet, $interFollowSet) {$userInfoList = array();foreach($findSet as $userID => $favoTime) {if(!in_array($userID, array_keys($userInfoArr))) continue;$userInfo = new UserInfo($userInfoArr[$userID]);$userInfo = $userInfo->format();if(in_array($userID, $fofaSet)){$userInfo[‘favoFlag‘] = 3; //互相关注} else {if($isSelf) {$userInfo[‘favoFlag‘] = $findType;} else {if(in_array($userID, $interFansSet)) {$userInfo[‘favoFlag‘] = 2; //我的粉丝} else if(in_array($userID, $interFollowSet)) {$userInfo[‘favoFlag‘] = 1; //我的关注} else{$userInfo[‘favoFlag‘] = 0; //无关系}}}$userInfo[‘favoTime‘] = $favoTime;array_push($userInfoList, $userInfo);}return $userInfoList;}
Using Redis to achieve Weibo attention relationships