如何用Redis实现微博关注关系

数据库 其他数据库 数据库运维 Redis
在微博中,每一个用户都会有一个关注列表,一个粉丝列表。用户可以查看自己的关注,粉丝列表,也可以查看别人的关注,粉丝列表。并且,要展示列表里每个人与当前查看者的关注状态。

关注关系产生的四种关系状态

  • 关注
  • 粉丝
  • 双向关注(互粉)
  • 无关系

需求分析

在微博中,每一个用户都会有一个关注列表,一个粉丝列表。用户可以查看自己的关注,粉丝列表,也可以查看别人的关注,粉丝列表。并且,要展示列表里每个人与当前查看者的关注状态。状态的可能性就是上面讲到得四种关系状态。

问题可以分两种情况来看:

  1. 看自己的关注,粉丝列表
  2. 看别人的关注,粉丝列表

看自己的关注,粉丝列表:

这种情况相对简单一点。比如看自己的关注列表,列表里的人的与自己的关系状态不可能是“无关系”和“粉丝”。只可能是“关注”和“双向关注”。同样,粉丝列表也只有两种状态。

看别人的关注,粉丝列表:

这是最复杂的情况,假如看别人关注列表,列表里的人和自己可能有上述全部四种关系状态。

从集合的图来分析

 


如上图所示。左边的圆表示用户的关注列表,右边的圆表示粉丝列表,下边的圆表示的是要查看的列表(集合)。分别用follow, fans, find来表明这三个集合。

当查看自己的列表时,其实表示find集合是上面集合中某一个的子集。例如查看自己粉丝,表示find是fans的子集,查看自己的关注,表示find是follow的子集。

查看别人的列表时,此时图中产生了三个集合的交集。要查询集合中的用户可能是在你的粉丝,关注集合中,也可能不在。就是说可能是任何一种关系状态,问题的根本就是,我们要计算出每一个用户与当前用户的关系状态。要求解四种关系状态,我们必然要求出图中下部分的三个小交集。

  • 要查询的集合与我的互粉交集
  • 要查询的集合与我的关注交集
  • 要查询的集的与我的粉丝交集

不在这三个小交集中的用户就是无关系状态的用户。

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


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

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

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

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

find中其他就是未关注

使用Sorted Set存储关系

score用来存储关注的时间,每个用户存储两个集合。follow:userID存储用户的关注,fans:userID存储用户的粉丝。于是我们可以设计一个函数来求出这些状态的集合。

函数返回:

  1. "findSet" => $findSet//要查询的集合 
  2. "fofaSet" => $fofaSet//互粉的集合 
  3. "findInterFollowSet" => $findInterFollowSet//要查询的集合与我的关注交 
  4. "findInterFansSet" => $findInterFansSet //要查询的集的与我的粉丝交 

 

求出以上四个集合,就可以进行关系状态判断,先判断是否互粉,如果不是互粉,再判断是否是我关注的,如果不是,再判断是否是我的粉丝。如果都不是就是无关系。这样就能把状态求出来了。

  1. /* 
  2. * userID:当前用户id 
  3. * targetUserID: 被查看的人的id 
  4. * findType: 查看的是哪个列表 
  5. * findStart: 分页查看的列表开始的位置 
  6. * findEnd: 分页查看的列表结束的位置 
  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.         //要查询的集合与我的关注交 
  24.         $findInterFollowSet = array(); 
  25.  
  26.         //要查询的集的与我的粉丝交 
  27.         $findInterFansSet = array(); 
  28.  
  29.         //先清掉临时集合 
  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: 要查询的集合与我的关注交 
  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") { //看自己关注列表 
  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//要查询的集合与我的关注交 
  72.             "findInterFansSet" => $findInterFansSet //要查询的集的与我的粉丝交 
  73.         ); 
  74.     } 

 

以上函数已经求出了所需要的集合,然后就是关系状态判断了。

 

  1. /* 
  2. * isSelf: 是否查看自己的列表 
  3. * findType: 查看的是粉丝还是关注列表 1: 关注, 2: 粉丝 
  4. * userInfoArr: 用户详细信息数组 
  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; //互相关注 
  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; //我的关注 
  26.                     } else
  27.                         $userInfo['favoFlag'] = 0; //无关系 
  28.                     } 
  29.                 } 
  30.                      
  31.             } 
  32.  
  33.             $userInfo['favoTime'] = $favoTime
  34.             array_push($userInfoList$userInfo); 
  35.         } 
  36.  
  37.         return $userInfoList
  38.     } 


 

责任编辑:Ophira 来源: oschina
相关推荐

2014-04-22 10:34:57

新浪微博Redis

2015-04-16 10:35:08

微博微博如何实现

2023-10-14 15:29:28

RedisFeed

2011-03-15 09:10:47

iptablesNAT

2022-08-11 18:27:50

面试Redis分布式锁

2021-08-08 22:08:41

Redis开发网页

2011-03-15 14:26:23

iptablesNAT

2019-09-25 17:12:44

2012-11-23 09:32:20

新浪微博微信

2014-10-10 15:16:38

HipHop数据挖掘

2013-10-10 09:05:26

新浪微博Redishadoop

2020-05-09 10:38:31

Python透视表数据

2012-04-13 09:51:56

火狐微博助手

2011-12-21 16:19:06

网秦手机安全微博保镖

2011-12-08 16:31:43

新浪微博开放平台

2020-11-02 08:15:00

Python数据开发

2014-09-25 15:51:14

微信企业号关注

2021-11-06 19:43:34

Python微信服务器

2011-08-15 13:42:57

2011-09-14 14:40:13

专业化微博IT微博
点赞
收藏

51CTO技术栈公众号