您将如何为用户实现“上次看到”的功能?

时间:2022-12-09 10:10:48

On Stack Overflow, the profile page lists a "last seen" property. This doesn't seem to be updated on every page view (for performance reasons, obviously). How would you implement it in a heavy-traffic web app? Would you update it only on certain pages? Or cache the last time you logged the user's last visit and wait a specific amount of time before updating the database? Or something completely different?

在Stack Overflow上,配置文件页面列出了“上次看到”的属性。这似乎没有在每个页面视图上更新(出于性能原因,显然)。您将如何在流量大的网络应用中实现它?你会仅在某些页面上更新吗?或者在上次记录用户上次访问时缓存并在更新数据库之前等待一段特定时间?还是完全不同的东西?

4 个解决方案

#1


On a heavy-traffic site like Stack Overflow, I would only update the "last seen" variable when a user actually does something. Lurking around and reading questions and answers shouldn't count as a user being "seen" by the system. Asking and answering questions, or voting on them should be actions that update when a user is last seen.

在像Stack Overflow这样流量大的网站上,我只会在用户真正做某事时更新“上次见过”的变量。潜伏并阅读问题和答案不应被视为系统“看到”的用户。询问和回答问题,或对它们进行投票应该是在上次看到用户时更新的操作。

I won't talk about the implementation details because that's already covered by other answers (and I would probably get it wrong).

我不会谈论实现细节,因为其他答案已经涵盖了(我可能会弄错)。

#2


You'll probably find "What strategy would you use for tracking user recent activity?" to be helpful. The issues are similar.

您可能会发现“您将使用什么策略来跟踪用户最近的活动?”有帮助。问题很相似。

#3


I would use a SESSION. And only set it the first visit of the session. Also resetting it every hour or so for if people leave the browser open. In php something like this:

我会用一个会话。并且只将它设置为会话的第一次访问。如果人们打开浏览器,也会每小时左右重置一次。在PHP这样的事情:

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

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

类似的东西,然后与OO,而不是两次做同样的事情。

#4


Consider using the "Command" design pattern for this. It will help you two ways - answer the question at hand and also implement an "undo/redo" feature. You should maintain a list of command objects designed per that pattern.

考虑使用“命令”设计模式。它将帮助您两种方式 - 回答手头的问题,并实现“撤消/重做”功能。您应该维护根据该模式设计的命令对象列表。

#1


On a heavy-traffic site like Stack Overflow, I would only update the "last seen" variable when a user actually does something. Lurking around and reading questions and answers shouldn't count as a user being "seen" by the system. Asking and answering questions, or voting on them should be actions that update when a user is last seen.

在像Stack Overflow这样流量大的网站上,我只会在用户真正做某事时更新“上次见过”的变量。潜伏并阅读问题和答案不应被视为系统“看到”的用户。询问和回答问题,或对它们进行投票应该是在上次看到用户时更新的操作。

I won't talk about the implementation details because that's already covered by other answers (and I would probably get it wrong).

我不会谈论实现细节,因为其他答案已经涵盖了(我可能会弄错)。

#2


You'll probably find "What strategy would you use for tracking user recent activity?" to be helpful. The issues are similar.

您可能会发现“您将使用什么策略来跟踪用户最近的活动?”有帮助。问题很相似。

#3


I would use a SESSION. And only set it the first visit of the session. Also resetting it every hour or so for if people leave the browser open. In php something like this:

我会用一个会话。并且只将它设置为会话的第一次访问。如果人们打开浏览器,也会每小时左右重置一次。在PHP这样的事情:

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

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

类似的东西,然后与OO,而不是两次做同样的事情。

#4


Consider using the "Command" design pattern for this. It will help you two ways - answer the question at hand and also implement an "undo/redo" feature. You should maintain a list of command objects designed per that pattern.

考虑使用“命令”设计模式。它将帮助您两种方式 - 回答手头的问题,并实现“撤消/重做”功能。您应该维护根据该模式设计的命令对象列表。