How to blog concerns relations with Redis

Four Relations concern the relationship between the state generated

  • attention
  • Fans
  • Bidirectional attention (mutual powder)
  • Unrelated

demand analysis

In the blog, each user will have a Watchlist, a fan list. Users can view their concerns, fan list, you can also view other people's attention, fan list. And, to show everyone concerned about the state of the list with the current viewer. The above mentioned is the possibility of state was four relationships state.

Problems can be divided into two situation:

  1. Look at their concerns, fan list
  2. Look attention, fan list of others

Look at their concerns, fan list:

This situation is relatively simple point. For example, look at the list of their concerns, the list of people and their relationship status can not be "unrelated" and "fans." It can only be "concerned" and "two-way concern." Similarly, the fan list and only two states.

Look attention, fan list of others:

This is the most complex case, if people look at your watchlist, the list of people you might have all of the above four relationship status.

From FIG set to analyze

As shown in FIG. Circle on the left represents the user's list of concerns, circle on the right represents the fan list, below the circle indicates the list you want to view (the collection). Respectively follow, fans, find to show three sets.

When viewing your own list, in fact, it represents a find is the set above a certain subset of. Such as viewing their own fans, fans expressed find a subset, see your concern, expressed find is a subset of follow.

When viewing the list of others, this time the figure had intersection of three sets. To query the user set might be your fans, followers, collection, or it may not. That could be any kind of relationship status, simply, we have to calculate each user relationship with the current state of the user's problem. State solution requires four relationships, we inevitably requires the intersection of three small figure next section.

  • To find a collection of my interaction with the intersection of powder
  • To find a collection of my concern with the intersection
  • To query the set with my fans intersection

This is not the intersection of three small user is unrelated to the state of the user.

If we adopt the following set of naming:

关注集合
follow:userID 粉丝集合 fans:userID

互粉集合(临时)
fofa:userID 要查询的集合(临时) find:userID

要查询的集合与我的关注交集(临时)
find_inter_follow:userID 要查询的集的与我的粉丝交集(临时) find_inter_fans:userID

要查询的集合与我的互粉交集(临时)
find_inter_fofa:userID

find中其他就是未关注

Use Sorted Set storage relationship

score is used to store the time concerned, each user to store two sets. follow: userID store the user's attention, fans: fans userID storage users. So we can design a function to obtain a set of these states.

The function returns:

"findSet" => $findSet, //要查询的集合 
"fofaSet" => $fofaSet, //互粉的集合 
"findInterFollowSet" => $findInterFollowSet, //要查询的集合与我的关注交 
"findInterFansSet" => $findInterFansSet //要查询的集的与我的粉丝交 

Obtaining more than four sets, can be carried out to determine the relationship between the state, first determine whether mutual powder, if not mutual powder, and then determine whether it is my concern, if not, then judge whether my fans. If that is not unrelated. Such a state will be able to find out.

/* 
* userID:当前用户id 
* targetUserID: 被查看的人的id 
* findType: 查看的是哪个列表 
* findStart: 分页查看的列表开始的位置 
* findEnd: 分页查看的列表结束的位置 
*/ 
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; 

        //找出要查询的集合元素 
        $findSet = $redis->zRevRange($targetKey, $findStart, $findEnd, TRUE); 

        //要查询的集合与我的关注交 
        $findInterFollowSet = array(); 

        //要查询的集的与我的粉丝交 
        $findInterFansSet = array(); 

        //先清掉临时集合 
        $redis->del($findKey); 

        $redis->del($fofaKey); 
        $redis->del($findInterFollowKey); 
        $redis->del($findInterFansKey); 

        //存起来 
        foreach ($findSet as $uid => $score) { 
            $redis->zAdd($findKey, $score, $uid); 
        } 

        //求互粉集合 
        if ($userID != $targetUserID) { //看别人 
            $redis->zInter($fofaKey, array($findKey, $fansKey, $followKey)); 

            /* 
             * 如果不是看自己的列表,还要求 
             * 1: 要查询的集合与我的关注交 
             * 2: 要查询的集的与我的粉丝交 
             */ 
            $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") { //自己看粉丝列表 
                $redis->zInter($fofaKey, array($findKey, $followKey)); 
            } else if ($findType == "follow") { //看自己关注列表 
                $redis->zInter($fofaKey, array($findKey, $fansKey)); 
            } 
        } 

        //互粉集合 
        $fofaSet = $redis->zRevRange($fofaKey, 0, -1); 

        return array( 
            "findSet" => $findSet, //要查询的集合 
            "fofaSet" => $fofaSet, //互粉的集合 
            "findInterFollowSet" => $findInterFollowSet, //要查询的集合与我的关注交 
            "findInterFansSet" => $findInterFansSet //要查询的集的与我的粉丝交 
        ); 
    } 

Function has been obtained above the required set, then the state is determined the relationship.

/* 
* 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; 
    } 

Guess you like

Origin blog.51cto.com/14230003/2461356