Yun Qi Hao: https://www.aliyun.com/#module-yedOfott8
The first-hand cloud information, the selected cloud enterprise case base of different industries, and the best practices extracted from many successful cases help you to make cloud decision!
Four kinds of relationship states of concern
- follow
- Fans
- Two way attention (mutual powder)
- No relationship
requirement analysis
In Weibo, every user will have a list of followers and fans. Users can view their own attention, fans list, and other people's attention, fans list. Also, show the attention status of each person in the list and the current viewer. The possibility of States is that we can get four kinds of relation states.
The problem can be divided into two situations:
- Watch your attention, fans list
- See other people's attention, fans list
Look at your attention, fan list:
This is relatively simple. For example, if you look at your list of concerns, the relationship between the people in the list and yourself cannot be "unrelated" or "fans". It can only be "attention" and "two-way attention". Similarly, there are only two states in the fan list.
See other people's attention, fans list:
This is the most complicated situation. If you look at the list of other people's concerns, the people in the list may have all four relationship states mentioned above.
Analysis from the graph of set
As shown above. The circle on the left represents the list of users' attention, the circle on the right represents the list of fans, and the circle on the bottom represents the list (set) to be viewed. Follow, fans and find are used to show the three sets.
When you view your own list, it means that the find set is a subset of one of the above sets. For example, to view your fans means that find is a subset of fans, to view your concerns, and to show that find is a subset of follow.
When viewing other people's lists, the intersection of three sets is generated in the graph. The users in the collection to be queried may be in your fans, focus on the collection, or not. That is to say, it may be any relationship state. The root of the problem is that we need to calculate the relationship state between each user and the current user. In order to solve four kinds of relation States, we must draw three small intersections of the lower part of the graph.
- The set to query intersects with my mutual pollination
- The set to query intersects with my concerns
- The set to query intersects with my fans
Users who are not in these three small intersections are unrelated users.
If we use the following set of naming >:
Focus set
follow:userID
Fans collection
fans:userID
Mutual powder collection (temporary)
fofa:userID
Collection to query (temporary)
find:userID
Intersection of collection to query and my concerns (temporary)
find_inter_follow:userID
Intersection of the set to query with my fans (temporary)
find_inter_fans:userID
Set to query intersects with my mutual powder (temporary)
find_inter_fofa:userID
The others in find are not concerned
Using Sorted Set to store relationships
score is used to store the time of concern. Each user stores two sets. follow:userID stores users' concerns, fans:userID stores users' fans. So we can design a function to find the set of these states.
Function returns:
"findSet" => $findSet, //Collection to query "fofaSet" => $fofaSet, //A collection of coprins "findInterFollowSet" => $findInterFollowSet, //Sets to query and my concerns "findInterFansSet" => $findInterFansSet //The set to be inquired and my fans
After finding the above four sets, we can judge the relationship state. First, we can judge whether we are fans of each other. If not, then we can judge whether we are concerned about each other. If not, then we can judge whether we are fans of each other. If not, it doesn't matter. So we can work out the state.
/* * userID:Current user id * targetUserID: id of the person viewed * findType: Which list to view * findStart: Where to start the list of paged views * findEnd: Location of the end of the paged view list */ 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 elements to query $findSet = $redis->zRevRange($targetKey, $findStart, $findEnd, TRUE); //Sets to query and my concerns $findInterFollowSet = array(); //The set to be inquired and my fans $findInterFansSet = array(); //Clear the temporary assembly first $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 mutual powder set if ($userID != $targetUserID) { //Look at others $redis->zInter($fofaKey, array($findKey, $fansKey, $followKey)); /* * If you don't look at your own list, you need to * 1: Sets to query and my concerns * 2: The set to be inquired and 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 the list of fans by yourself $redis->zInter($fofaKey, array($findKey, $followKey)); } else if ($findType == "follow") { //Look at your list of concerns $redis->zInter($fofaKey, array($findKey, $fansKey)); } } //Reciprocal powder set $fofaSet = $redis->zRevRange($fofaKey, 0, -1); return array( "findSet" => $findSet, //Collection to query "fofaSet" => $fofaSet, //A collection of coprins "findInterFollowSet" => $findInterFollowSet, //Sets to query and my concerns "findInterFansSet" => $findInterFansSet //The set to be inquired and my fans ); }
The above function has found the required set, and then the relationship state judgment.
/* * isSelf: Whether to view your own list * findType: Fans or follow list 1: follow, 2: Fans * userInfoArr: User details array */ 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; //Mutual concern } else { if($isSelf) { $userInfo['favoFlag'] = $findType; } else { if(in_array($userID, $interFansSet)) { $userInfo['favoFlag'] = 2; //My fans } else if(in_array($userID, $interFollowSet)) { $userInfo['favoFlag'] = 1; //My concern } else{ $userInfo['favoFlag'] = 0; //No relationship } } } $userInfo['favoTime'] = $favoTime; array_push($userInfoList, $userInfo); } return $userInfoList; }
This is the introduction of using Redis to realize Weibo attention relationship. Welcome to exchange and discuss.
Original release time: December 24, 2019
Author: jockchau; Editor: Alopecia
This article is from Alibaba cloud Qihao partner“ CSDN cloud computing ”, you can pay attention to“ CSDN cloud computing"
Yun Qi Hao: https://www.aliyun.com/#module-yedOfott8
The first-hand cloud information, the selected cloud enterprise case base of different industries, and the best practices extracted from many successful cases help you to make cloud decision!