比如,我本身已经关注了a,b,c
那sql可能这样的
$db->select('select * from posts where authorid in (a,b,c) order by timestamp desc limit n');
但是,假如我关注了1000个人呢?10000个呢?
使用redis如何操作呢?
记事狗微博是这么干的
select tid from jishigou_topic where 1 and uid in('1','2') and `type` in('first','forward','both') and `dateline`>1366483026 order by `dateline` desc limit 1000 replace into jishigou_cache_7 set `key`='1-topic-myhome--0',`dateline`='1369075026',`val`='ytoyontzojq6imrhdgeio2e6mjp7czo0oijsaxn0ijthoje6e2k6mdthojq6e2k6mdtzoje6ijqio2k6mttzoje6ijmio2k6mjtzoje6ijiio2k6mztzoje6ijeio319czo1oijjb3vudci7ato0o31zojq6imxpzmuio2k6njawo30='select * from jishigou_topic where `tid` in ('4','3','2','1')
回复内容: 比如,我本身已经关注了a,b,c
那sql可能这样的
$db->select('select * from posts where authorid in (a,b,c) order by timestamp desc limit n');
但是,假如我关注了1000个人呢?10000个呢?
使用redis如何操作呢?
记事狗微博是这么干的
select tid from jishigou_topic where 1 and uid in('1','2') and `type` in('first','forward','both') and `dateline`>1366483026 order by `dateline` desc limit 1000 replace into jishigou_cache_7 set `key`='1-topic-myhome--0',`dateline`='1369075026',`val`='ytoyontzojq6imrhdgeio2e6mjp7czo0oijsaxn0ijthoje6e2k6mdthojq6e2k6mdtzoje6ijqio2k6mttzoje6ijmio2k6mjtzoje6ijiio2k6mztzoje6ijeio319czo1oijjb3vudci7ato0o31zojq6imxpzmuio2k6njawo30='select * from jishigou_topic where `tid` in ('4','3','2','1')
yy一下如果是我,并且是用redis,我会如何解决这个问题。
数据类型选择我会选择用list存储好友的微博,有两个好处:
插入很快,时间复杂度o(1)在数据量级比较小的时候(譬如10-20条)查找也很快,时间复杂度0(n)执行过程:假如a有三个好友,b、c、d,然后a发了一条微博:hello, 这时候a b c d 理论上都能够马上看到这条新微博,执行过程如下:
a往自己的微博列表中插入这条hello消息,lpush 或者 rpush往b c d的微博列表中分别插入这条hello消息,lpush 或者 rpush当用户打开或者刷新页面时,就能看到好友的新鲜事了,但这又一个问题:如果a有1w个好友,那他岂不是要往1w个好友的微博列表中插入hello消息,那页面非卡死不可! 这个问题如何解决呢?
我觉得是这样的: 首先自己发新微博自己一定要立即看到,但是其他好友可以稍微慢个几s看到也没多大关系,所以首先往自己的微博列表中插入信息是必须的,然后更新好友微博列表操作可以采用消息的方式异步化掉,也就是说新的改进过程如下:
往自己的微博列表中插入这条hello消息,lpush 或者 rpush发送更新信息: update b c d hello当某个消息接收器收到update信息时,就会去更新响应人的微博列表
以上内容,纯属yy