偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何用Redis實(shí)現(xiàn)微博關(guān)注關(guān)系

數(shù)據(jù)庫 其他數(shù)據(jù)庫 數(shù)據(jù)庫運(yùn)維 Redis
在微博中,每一個(gè)用戶都會有一個(gè)關(guān)注列表,一個(gè)粉絲列表。用戶可以查看自己的關(guān)注,粉絲列表,也可以查看別人的關(guān)注,粉絲列表。并且,要展示列表里每個(gè)人與當(dāng)前查看者的關(guān)注狀態(tài)。

關(guān)注關(guān)系產(chǎn)生的四種關(guān)系狀態(tài)

  • 關(guān)注
  • 粉絲
  • 雙向關(guān)注(互粉)
  • 無關(guān)系

需求分析

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

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

  1. 看自己的關(guān)注,粉絲列表
  2. 看別人的關(guān)注,粉絲列表

看自己的關(guān)注,粉絲列表:

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

看別人的關(guān)注,粉絲列表:

這是最復(fù)雜的情況,假如看別人關(guān)注列表,列表里的人和自己可能有上述全部四種關(guān)系狀態(tài)。

從集合的圖來分析

 


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

當(dāng)查看自己的列表時(shí),其實(shí)表示find集合是上面集合中某一個(gè)的子集。例如查看自己粉絲,表示find是fans的子集,查看自己的關(guān)注,表示find是follow的子集。

查看別人的列表時(shí),此時(shí)圖中產(chǎn)生了三個(gè)集合的交集。要查詢集合中的用戶可能是在你的粉絲,關(guān)注集合中,也可能不在。就是說可能是任何一種關(guān)系狀態(tài),問題的根本就是,我們要計(jì)算出每一個(gè)用戶與當(dāng)前用戶的關(guān)系狀態(tài)。要求解四種關(guān)系狀態(tài),我們必然要求出圖中下部分的三個(gè)小交集。

  • 要查詢的集合與我的互粉交集
  • 要查詢的集合與我的關(guān)注交集
  • 要查詢的集的與我的粉絲交集

不在這三個(gè)小交集中的用戶就是無關(guān)系狀態(tài)的用戶。

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


關(guān)注集合
follow:userID 粉絲集合 fans:userID

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

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

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

find中其他就是未關(guān)注

使用Sorted Set存儲關(guān)系

score用來存儲關(guān)注的時(shí)間,每個(gè)用戶存儲兩個(gè)集合。follow:userID存儲用戶的關(guān)注,fans:userID存儲用戶的粉絲。于是我們可以設(shè)計(jì)一個(gè)函數(shù)來求出這些狀態(tài)的集合。

函數(shù)返回:

  1. "findSet" => $findSet//要查詢的集合 
  2. "fofaSet" => $fofaSet//互粉的集合 
  3. "findInterFollowSet" => $findInterFollowSet//要查詢的集合與我的關(guān)注交 
  4. "findInterFansSet" => $findInterFansSet //要查詢的集的與我的粉絲交 

 

求出以上四個(gè)集合,就可以進(jìn)行關(guān)系狀態(tài)判斷,先判斷是否互粉,如果不是互粉,再判斷是否是我關(guān)注的,如果不是,再判斷是否是我的粉絲。如果都不是就是無關(guān)系。這樣就能把狀態(tài)求出來了。

  1. /* 
  2. * userID:當(dāng)前用戶id 
  3. * targetUserID: 被查看的人的id 
  4. * findType: 查看的是哪個(gè)列表 
  5. * findStart: 分頁查看的列表開始的位置 
  6. * findEnd: 分頁查看的列表結(jié)束的位置 
  7. */ 
  8. function getChunkSets($redis$userID$targetUserID$findType$findStart$findEnd) { 
  9.  
  10.         $fansKey = "fans:" . $userID
  11.         $followKey = "follow:" . $userID
  12.         $findKey = "find:" . $userID
  13.  
  14.         $targetKey =  $findType":" . $targetUserID
  15.         $fofaKey = "find_inter_fofa:" . $userID
  16.  
  17.         $findInterFollowKey = "find_inter_follow:" . $userID
  18.         $findInterFansKey = "find_inter_fans:" . $userID
  19.  
  20.         //找出要查詢的集合元素 
  21.         $findSet = $redis->zRevRange($targetKey$findStart$findEnd, TRUE); 
  22.  
  23.         //要查詢的集合與我的關(guān)注交 
  24.         $findInterFollowSet = array(); 
  25.  
  26.         //要查詢的集的與我的粉絲交 
  27.         $findInterFansSet = array(); 
  28.  
  29.         //先清掉臨時(shí)集合 
  30.         $redis->del($findKey); 
  31.  
  32.         $redis->del($fofaKey); 
  33.         $redis->del($findInterFollowKey); 
  34.         $redis->del($findInterFansKey); 
  35.  
  36.  
  37.         //存起來 
  38.         foreach ($findSet as $uid => $score) { 
  39.             $redis->zAdd($findKey$score$uid); 
  40.         } 
  41.  
  42.         //求互粉集合 
  43.         if ($userID != $targetUserID) { //看別人 
  44.             $redis->zInter($fofaKeyarray($findKey$fansKey$followKey)); 
  45.  
  46.             /* 
  47.              * 如果不是看自己的列表,還要求 
  48.              * 1: 要查詢的集合與我的關(guān)注交 
  49.              * 2: 要查詢的集的與我的粉絲交 
  50.              */ 
  51.             $redis->zInter($findInterFollowKeyarray($findKey$followKey)); 
  52.             $redis->zInter($findInterFansKeyarray($findKey$fansKey)); 
  53.  
  54.             $findInterFollowSet = $redis->zRevRange($findInterFollowKey, 0, -1); 
  55.             $findInterFansSet = $redis->zRevRange($findInterFansKey, 0, -1); 
  56.  
  57.         } else { 
  58.             if ($findType == "fans") { //自己看粉絲列表 
  59.                 $redis->zInter($fofaKeyarray($findKey$followKey)); 
  60.             } else if ($findType == "follow") { //看自己關(guān)注列表 
  61.                 $redis->zInter($fofaKeyarray($findKey$fansKey)); 
  62.             } 
  63.         } 
  64.  
  65.         //互粉集合 
  66.         $fofaSet = $redis->zRevRange($fofaKey, 0, -1); 
  67.  
  68.         return array
  69.             "findSet" => $findSet//要查詢的集合 
  70.             "fofaSet" => $fofaSet//互粉的集合 
  71.             "findInterFollowSet" => $findInterFollowSet//要查詢的集合與我的關(guān)注交 
  72.             "findInterFansSet" => $findInterFansSet //要查詢的集的與我的粉絲交 
  73.         ); 
  74.     } 

 

以上函數(shù)已經(jīng)求出了所需要的集合,然后就是關(guān)系狀態(tài)判斷了。

 

  1. /* 
  2. * isSelf: 是否查看自己的列表 
  3. * findType: 查看的是粉絲還是關(guān)注列表 1: 關(guān)注, 2: 粉絲 
  4. * userInfoArr: 用戶詳細(xì)信息數(shù)組 
  5. */ 
  6. function getUserInfoList($isSelf$findType$userInfoArr$findSet$fofaSet$interFansSet$interFollowSet) { 
  7.  
  8.         $userInfoList = array(); 
  9.  
  10.         foreach($findSet as $userID => $favoTime) { 
  11.             if(!in_array($userIDarray_keys($userInfoArr))) continue
  12.  
  13.             $userInfo = new UserInfo($userInfoArr[$userID]); 
  14.             $userInfo = $userInfo->format(); 
  15.  
  16.             if(in_array($userID$fofaSet)){ 
  17.                 $userInfo['favoFlag'] = 3; //互相關(guān)注 
  18.             } else { 
  19.                 if($isSelf) { 
  20.                     $userInfo['favoFlag'] = $findType
  21.                 } else { 
  22.                     if(in_array($userID$interFansSet)) { 
  23.                         $userInfo['favoFlag'] = 2; //我的粉絲 
  24.                     } else if(in_array($userID$interFollowSet)) { 
  25.                         $userInfo['favoFlag'] = 1; //我的關(guān)注 
  26.                     } else
  27.                         $userInfo['favoFlag'] = 0; //無關(guān)系 
  28.                     } 
  29.                 } 
  30.                      
  31.             } 
  32.  
  33.             $userInfo['favoTime'] = $favoTime
  34.             array_push($userInfoList$userInfo); 
  35.         } 
  36.  
  37.         return $userInfoList
  38.     } 


 

責(zé)任編輯:Ophira 來源: oschina
相關(guān)推薦

2014-04-22 10:34:57

新浪微博Redis

2015-04-16 10:35:08

微博微博如何實(shí)現(xiàn)

2023-10-14 15:29:28

RedisFeed

2024-06-11 10:03:56

2024-10-14 14:19:02

2019-09-25 17:12:44

2011-03-15 09:10:47

iptablesNAT

2011-03-15 14:26:23

iptablesNAT

2021-08-08 22:08:41

Redis開發(fā)網(wǎng)頁

2012-11-23 09:32:20

新浪微博微信

2022-08-11 18:27:50

面試Redis分布式鎖

2014-10-10 15:16:38

HipHop數(shù)據(jù)挖掘

2011-12-21 16:19:06

網(wǎng)秦手機(jī)安全微博保鏢

2011-12-08 16:31:43

新浪微博開放平臺

2013-10-10 09:05:26

新浪微博Redishadoop

2012-04-13 09:51:56

火狐微博助手

2024-12-19 10:00:00

Python發(fā)送消息編程

2024-09-03 13:22:33

2011-09-14 14:40:13

專業(yè)化微博IT微博

2015-09-24 18:08:50

微博架構(gòu)架構(gòu)演進(jìn)架構(gòu)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號