使用Redis的通知和新闻区域 [英] Notification and News Area by using Redis

查看:71
本文介绍了使用Redis的通知和新闻区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相册系统,人们可以上传照片并与其他用户互动.我使用mySql和Redis处理流量并存储数据.

I have a photo album system which people can upload photos and interract with other users. I use mySql and Redis to handle traffic and store the data.

在我的系统中,用户可以关注其他用户,例如照片,对其进行评论并上传新照片.

In my system, users can follow other users, like photos, comment on them and upload new photos.

在这种情况下,我想在其墙上显示来自用户关注者的所有事件(例如Facebook时间轴).

In this scenario, I want to show all events from user's followings on their wall (like facebook timeline).

例如,我关注用户30、40、50、60、70和80个ID.每当他们添加新照片时,我都想在我的墙上看到它们.问题是这样的:

For example, I follow users 30,40,50,60,70 and 80 ids. Whenever they add a new photo, I would like to see them on my wall. The problem is this :

用户30(他的ID)添加了3张新照片,我将此信息添加到了photoevents:30:photoids-> [55,56,57]中.在我的墙上看到的是这样的:

User 30 (his id) added 3 new photos and I added this information to photoevents:30:photoids -> [55,56,57]. It is seen on my wall like this:

User 30 added 3 new photos [55,56,57]

然后用户40添加了2张新照片[5,6]在我的墙上看到的是这样的:

Then user 40 added 2 new photos [5,6] It is seen on my wall like this:

 User 40 added 2 new photos [5,6].     
 User 30 added 3 new photos [55,56,57]

然后用户30添加了2张照片[58,59]

Then user 30 added 2 more photos [58,59]

应该看到什么?

喜欢

 User 30 added 5 new photos [55,56,57,58,59].     
 User 40 added 2 new photos [5,6]

 User 30 added 2 new photos [58,59]
 User 40 added 2 new photos [5,6].     
 User 30 added 3 new photos [55,56,57]

对于第二种情况,我怎么知道墙所有者看到的最后一张照片是什么?根据什么,我应该确定新"数字.哪一种是合理的方法?将此数据保留在服务器端(对追随者的整个最后一次查看"组合(例如:用户30的最近一次查看ID,由用户50等)或在客户端进行处理

For the second case, how can I know what was the last photo that the wall owner seen ? According to what, I should decide the "new" number. Which one is the reasonable approach; keeping this data at server side (whole "last seen" combinations for followers (ex: last seen id of user 30 by user 50 etc.) or handling this on client side

推荐答案

我认为此功能应在服务器端处理,尤其是在您希望用户使用多种类型的客户端(例如浏览器,电话等)的情况下

In my opinion this function should be handled server side, especially if you expect users to use multiple types of clients (e.g., browser, phone etc.)

每个事件都应该有一个时间戳记,以便知道每个事件何时发生,并能够相应地进行范围搜索,以防万一,例如,您需要获取事件的最后24小时或之后的事件时间戳X.

Each of your events should have a timestamp in order to know when each event happened and in order to be able to do range searches accordingly, in case you need to get, for example the last 24 hours of events, or events after timestamp X.

然后,当跟随另一个用户的用户查看其通知时,您可以将时间戳记设置为检查点,以了解该用户已经看到的事件,然后仅显示该检查点之后的事件.

Then when the user that follows another user looks at her notifications you can set a timestamp as a checkpoint to know up to which event this user has already seen and then only show the events that are after that checkpoint.

我在一个项目中的REDIS中实现时间序列的方式是使用排序集( http://redis.io/topics/data-types#sorted-sets ).在您的情况下,您可以存储:

The way I have implemented timeseries in REDIS in one of our projects is by using Sorted Sets (http://redis.io/topics/data-types#sorted-sets). In your case you could store:

user1EventsKey -> [{ts1, eventKey1}, {ts2, eventKey2}, {ts3, eventKey3},... ]  
                                                           # This is the sorted set
eventKey1 -> [photo1, photo2, photo3]
eventKey2 -> [photo4, photo5]
...

user2Checkpoint -> tsA   #where  ts2 < tsA < ts3

现在您知道,下次使用user2查看通知时,您将仅使用 ZRANGEBYSCORE user1EventsKey ts2 + inf 来获得tsA之后的事件,然后可以一个接一个地显示显示事件.

Now you know that the next time user2 views the notifications you'll only show whatever events are after tsA using ZRANGEBYSCORE user1EventsKey ts2 +inf to get all the event keys and then one by one you can display the events.

PS.时间戳可以以UNIX格式存储.

PS. The timestamp can be stored in the UNIX format.

我希望这会有所帮助.

这篇关于使用Redis的通知和新闻区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆