How would you implement the last seen feature for users?

In mode, the "last seen" property is displayed on the profile page. This does not seem to be updated on every pageview (obviously, for performance reasons). How to implement it in a web application with a lot of traffic? Would you update it only on certain pages? Or cache the last time you entered the last user visit and waited a certain amount of time before updating the database? Or something completely different?

+5
source share
4 answers

On a site with heavy traffic, such as Stack Overflow, I would only update the "last time" variable when the user really does something. When hiding and reading questions and answers, you should not be considered a user who "sees" the system. Ask and answer questions or vote for them should be actions that are updated when the user last saw.

I will not talk about implementation details because it is already covered by other answers (and probably I'm wrong).

+5
source

You will most likely find "What strategy would you use to track user activity recently?" . The problems are similar.

+3
source

I would take advantage of the SESSION. And just set it the first visit to the session. Also reset it every hour or so if people leave the browser open. In php something like this:

if(!isset(!_SESSION['lastSeen'])){
 $_SESSION['lastSeen'] = time();
 updateLastSeenInDatabaseOrSomething();
}
else{
 if($_SESSION['lastSeen'] < time() + 2 * 60 * 60){ //2 hours
  $_SESSION['lastSeen'] = time();
  updateLastSeenInDatabaseOrSomething();   
 }
}

Something like this, but then with OO and not doing the same thing twice.

+2
source

Consider using the Command design pattern for this. This will help you in two ways - answer the question, and also perform the cancel / redo function. You must keep a list of command objects designed for this template.

0
source

All Articles