Using Redis to realize micro-blogging concern Relationship _redis

Source: Internet
Author: User
Tags redis

Pay attention to the four relationship states that the relationship produces

    • Concern
    • Fans
    • Bidirectional attention (Mutual powder)
    • No relationship

Demand analysis

In Weibo, each user will have a list of concerns, a list of fans. Users can view their own attention, a list of fans, or a list of other people's concerns and fans. Also, show the state of concern for everyone in the list and the current viewer. The possibility of a state is the four relationship States mentioned above.

The problem can be seen in two ways:

1, see their own attention, the list of fans
2, see other people's attention, fan list
look at your own attention, List of fans:

This is a relatively simple situation. For example, look at their own list of concerns, lists of people's relationship with their own status can not be "no relationship" and "fans." It can only be "attention" and "two-way attention". Similarly, the list of fans has only two states.

See other people's attention, fans list:

This is the most complicated situation, if you look at other people's attention list, the table and the person may have all of the above four relationship status.

To analyze from the graph of a set

As shown in the figure above. The circle on the left represents the user's list of concerns, the circle on the right represents the list of fans, and the bottom circle represents the list to view (the collection). Use follow, fans, find to indicate these three sets.

When you view your list, it actually means that the Find collection is a subset of one of the above collections. For example, look at your fans, show that find is a subset of fans, see their own attention, to show a subset of follow.

When you view a list of others, the intersection of three sets is generated in this diagram. To query the users in the collection may be in your fans, focus on the collection, or maybe not. That is, it may be any kind of relationship state, and the problem is that we have to calculate the state of each user's relationship with the current user. In order to solve the four relationship states, we necessarily require the three small intersection of the lower part of the graph.

    • The collection to query is intersected with my cross powder
    • The collection to query and the intersection of my concerns
    • The set that I want to query intersects with my fans

Users who are not in these three small intersection sets are users who are not in a relational state.

Suppose we adopt the following set of names:

Focus on the collection
Follow:userid Fan Collection Fans:userid

Cross Powder Collection (temporary)
Fofa:userid the collection to query (temporary) Find:userid

The collection to query is the intersection of my concerns (temporary)
Find_inter_follow:userid the set that you want to query with my fan intersection (temp) find_inter_fans:userid

The collection to query is intersected with my cross (temporary)
Find_inter_fofa:userid

Find the other is not concerned about

Using the sorted set storage relationship

Score is used to store the attention time, and each user stores two collections. 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,//The collection "Fofaset" to be queried 
=> $fofaSet,///The set of mutual powders 
"Findinterfollowset" => $ Findinterfollowset,//To query the collection with my attention to the 
"Findinterfansset" => $findInterFansSet//To query the set of with my fans pay 

To find out the above four sets, you can conduct the relationship between the state of judgment, the first to determine whether the mutual powder, if not the mutual powder, and then judge whether it is my concern, if not, then judge whether it is my fans. If it's not, it's nothing. So that we can find the state.

* * UserID: The current User ID * Targetuserid: ID of the person being viewed * Findtype: Which list is viewed * Findstart: Page view where the list begins * Findend: 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 Set element to query $findSet = $redis->zrevrange ($targetKey, $findStart, $findEnd, TRUE); 
 
 To query the collection with my concern $findInterFollowSet = Array (); 
 
 To query the set of with my fans $findInterFansSet = array (); 
 
 Clear out 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); The!= collection if ($userID $targeTuserid) {//See others $redis->zinter ($fofaKey, Array ($findKey, $fansKey, $followKey)); * * If not to see their own list, also requires * 1: To query the set with my attention to pay * 2: To query the set with my fans/$redis->zinter ($findInterFollowKey, Array ($fi 
  Ndkey, $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") {//Look at your own list of concerns $redis->zinter ($fofaKey, Array ($findKey, $fansKey)); 
 
 }////$fofaSet = $redis->zrevrange ($fofaKey, 0,-1); Return Array ("Findset" => $findSet,//The collection "Fofaset" that you want to query => $fofaSet,///The Set "Findinterfollowset" of the mutual powder => $fi 
 Ndinterfollowset,//want to query the collection with my attention to pay "Findinterfansset" => $findInterFansSet//To query the set of with my fans to pay); 
 }

The above function has already obtained the desired set, and then the relationship state is judged.

 * * Isself: Do you want to view your list * Findtype: See fans or attention List 1: Attention, 2: Fans * Userinfoarr: User details array/FU Nction getuserinfolist ($isSelf, $findType, $USERINFOARR, $findSet, $fofaSet, $interFansSet, $interFollowSet) {$userIn 
 
 Folist = 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 attention} else {if ($isSelf) {$userInfo [' Favoflag 
  '] = $findType; else {if (In_array ($userID, $interFansSet)) {$userInfo [' favoflag '] = 2;//My Fan} else if (In_array ($userID, 
   
  $interFollowSet)) {$userInfo [' favoflag '] = 1;//My concerns} else{$userInfo [' favoflag '] = 0;//no Relationship}} 
  $userInfo [' favotime '] = $favoTime; 
 Array_push ($userInfoList, $userInfo); 
 return $userInfoList; } 

How to use Redis to achieve micro-blogging concerns about this, welcome to discuss the exchange.

Related Article

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.