I have writt开发者_运维问答en a simple forum in PHP using PostgreSQL. The forum consists of a number of subforums (or categories, if you like) that contain topics. I have a table that stores when was the last time a user visited a topic. It's something like this: user_id, topic_id, timestamp.
I can easily determine what topics should be marked as unread by comparing the timestamp of the last topic reply with the timestamp of the last user visit.
My question is: how do I efficiently determine what subforums (categories) should be marked as unread? All I've come up with is this: every time a user visits a topic, update the visit timestamp and check if all the topics from the current subforum are read or unread. If they are all read, mark the subforum as read for the user. Else, mark it as unread. But I think there must be another way.
Thank you in advance.
There are many ways (like yours) to achieve a similar behavior, since you mention efficiency I will consider performance is important.
The way I handled this before did not involved a database to take care of unread content at all. That in mind, my suggestion would be:
- On the first visit mark only topics newer than, let's say, 3 days as 'unread'
- As the user browses the topics, start throwing the topic IDs and LastUpdate for the thread into a cookie on the client
- When the forum pages load, check the cookie and if the thread has suffered any updates, this code and also the cookie handling can be easily done with pure javascript.
- If the client is a whole week away from the website, no problem, he will see everything newer than 3 days (first visit rule) as unread.
p.s.: This is 100% related to how important is to a person to know what he has not read. In my suggestion this is not something crucial, because it is not 100% reliable (we are not using a database/proper persistance after all)
精彩评论