问题
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