Notification and News Area by using Redis

纵然是瞬间 提交于 2021-02-11 18:16:17

问题


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.

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

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 :

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]

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]

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

What should it be seen ?

Like that

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

Or

 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]

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


回答1:


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.)

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.

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

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. The timestamp can be stored in the UNIX format.

I hope this helps.



来源:https://stackoverflow.com/questions/21833700/notification-and-news-area-by-using-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!