用 Redis 實現微博關注關系

jopen 10年前發布 | 51K 次閱讀 Redis NoSQL數據庫

用Redis實現微博關注關系的分析

關注關系產生的四種關系狀態

  • 關注
  • 粉絲
  • 雙向關注(互粉)
  • 無關系
  • </ul>

    需求分析

    在微博中,每一個用戶都會有一個關注列表,一個粉絲列表。用戶可以查看自己的關注,粉絲列表,也可以查看別人的關注,粉絲列表。并且,要展示列表里每個人與當前查看者的關注狀態。狀態的可能性就是上面講到得四種關系狀態。

    問題可以分兩種情況來看:

    1. 看自己的關注,粉絲列表
    2. 看別人的關注,粉絲列表
    3. </ol>

      看自己的關注,粉絲列表:

      這種情況相對簡單一點。比如看自己的關注列表,列表里的人的與自己的關系狀態不可能是“無關系”和“粉絲”。只可能是“關注”和“雙向關注”。同樣,粉絲列表也只有兩種狀態。

      看別人的關注,粉絲列表:

      這是最復雜的情況,假如看別人關注列表,列表里的人和自己可能有上述全部四種關系狀態。

      從集合的圖來分析

      用 Redis 實現微博關注關系

      如上圖所示。左邊的圓表示用戶的關注列表,右邊的圓表示粉絲列表,下邊的圓表示的是要查看的列表(集合)。分別用follow, fans, find來表明這三個集合。

      當查看自己的列表時,其實表示find集合是上面集合中某一個的子集。例如查看自己粉絲,表示find是fans的子集,查看自己的關注,表示find是follow的子集。

      查看別人的列表時,此時圖中產生了三個集合的交集。要查詢集合中的用戶可能是在你的粉絲,關注集合中,也可能不在。就是說可能是任何一種關系狀態,問題的根本就是,我們要計算出每一個用戶與當前用戶的關系狀態。要求解四種關系狀態,我們必然要求出圖中下部分的三個小交集。

      • 要查詢的集合與我的互粉交集
      • 要查詢的集合與我的關注交集
      • 要查詢的集的與我的粉絲交集
      • </ul>

        不在這三個小交集中的用戶就是無關系狀態的用戶。

        假如我們采用如下一套命名:

        關注集合follow:userID 粉絲集合 fans:userID

        互粉集合(臨時)fofa:userID 要查詢的集合(臨時) find:userID

        要查詢的集合與我的關注交集(臨時)find_inter_follow:userID 要查詢的集的與我的粉絲交集(臨時) find_inter_fans:userID

        要查詢的集合與我的互粉交集(臨時)find_inter_fofa:userID

        find中其他就是未關注

        </div>

        使用Sorted Set存儲關系

        score用來存儲關注的時間,每個用戶存儲兩個集合。follow:userID存儲用戶的關注,fans:userID存儲用戶的粉絲。于是我們可以設計一個函數來求出這些狀態的集合。

        函數返回:

        "findSet" => $findSet, //要查詢的集合
        "fofaSet" => $fofaSet, //互粉的集合
        "findInterFollowSet" => $findInterFollowSet, //要查詢的集合與我的關注交
        "findInterFansSet" => $findInterFansSet //要查詢的集的與我的粉絲交

        求出以上四個集合,就可以進行關系狀態判斷,先判斷是否互粉,如果不是互粉,再判斷是否是我關注的,如果不是,再判斷是否是我的粉絲。如果都不是就是無關系。這樣就能把狀態求出來了。

        /*

        • 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 //要查詢的集的與我的粉絲交
            );
        }</pre> <p>以上函數已經求出了所需要的集合,然后就是關系狀態判斷了。 </p>
        

        /*

        • 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;
          

          }</pre>原文 http://my.oschina.net/jockchou/blog/473598

         本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
         轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
         本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!