跟踪用户活动的最具可扩展性的方法是什么?

发布于 2024-10-09 06:35:57 字数 533 浏览 0 评论 0原文

我想跟踪某些用户活动并使用时间限制来限制此活动。一个例子是 SO 强制执行的每日投票限制。以下哪种方法在可扩展性和易用性方面提供了更好的解决方案?

  • 将活动信息存储在数据库中
  • 将活动信息存储在应用程序范围中
  • 使用完全不同的方法来跟踪此信息

方法 1:

  • 优点:数据完整性,这在我的应用程序中并不重要用例。
  • 缺点:需要数据库交互、存储冗余数据、需要计划事件来重置正在跟踪的值

方法 2

  • 优点:不需要数据库交互,似乎更容易
  • 缺点:我不确定考虑到需要跟踪的大量用户群对应用程序内存的影响,或者使用数百个键查找和操作结构数据有多容易

。我将 ColdFusion 与 MySQL 一起使用,如果这很重要的话。

I would like to track certain user activity and limit this activity using a time constraint. An example of this could be the votes per day limit enforced by SO. Which of the following methods provides a better solution in terms of scalability and ease of use?

  • Store activity info in the database
  • Store activity info in the application scope
  • Use an entirely different method to track this information

Method 1:

  • Advantages: data integrity, which is not important in my use case.
  • Disadvantages: requires database interaction, stores redundant data, requires a scheduled event to reset the values being tracked

Method 2

  • Advantages: does not require database interaction, appears to be easier
  • Disadvantages: I'm unsure of the implications on application memory given a large user-base to track, or how easy it is to find and manipulate struct data with hundreds of keys

I'm using ColdFusion with MySQL, if this matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-10-16 06:35:57

假设您只关心每个人的数据,并且不会获得这些数据的聚合,我建议您使用支持 TTL 的键/值存储。 Redis 听起来很符合您的要求。

  • 虽然它在内存中运行,但它会定期向磁盘创建快照(这意味着它速度很快,如果出现故障,您将恢复大部分内容)。

  • 轻松交互,与“方法 2”一样,但不必担心管理大型内存结构。

示例设置

对于每日 SO 积分场景,我们的时间范围为一天,我们的值将是一个简单的值,会根据某些用户活动而增加。

我们的密钥将是用户 ID 和当前时间范围(在本例中为天)的组合。设置密钥后,我们设置一个 ttl,这意味着在今天结束后,密钥最终会过期并自行清理。

redis> incr user123-2011-10-09
(integer) 1  # no value found for key, defaulting to 1
             # if this key already had a value it would be incremented.

redis> expire user123-2011-10-09 86400
(integer) 1  # expiration of 1 day was successful

redis> get user123-2011-10-09
"1"          # current value of user key

有关这些命令的更多信息(您最感兴趣的 incr、get、expire 和 ttl)可以在 redis 命令中找到参考(使用交互式 shell 来尝试这些事情)。

编辑

这种方法显然可以应用于任意数量的 k/v 存储,我在这里使用 Redis 作为具体示例,因为我相信它非常满足要求。

Assuming you only care about this data on a per-person basis, and won't be getting aggregates of this, I'd suggest you go with a key/val store that supports TTL. Redis sounds like it fits your requirements pretty well.

  • Though it runs in memory, it does snapshot to disk periodically (which mean it's fast, and if something goes down, you'll get most everything back).

  • Easy interaction, as with your "method 2", but without having to worry about managing a large in-memory structure.

Example Setup

Going with the SO points-per-day scenario, we'll have a time-frame of one day, and our value will be a simple value that increments on some user activity.

Our key would be a combination of the user's id and the current timeframe (day, in this case). After we have set the key, we set a ttl, which means after today is up, the key will eventually expire and clean itself up.

redis> incr user123-2011-10-09
(integer) 1  # no value found for key, defaulting to 1
             # if this key already had a value it would be incremented.

redis> expire user123-2011-10-09 86400
(integer) 1  # expiration of 1 day was successful

redis> get user123-2011-10-09
"1"          # current value of user key

More info on these commands (incr, get, expire, and ttl would be of most interest to you) can be found on the redis commands reference (complete with an interactive shell to try these things out).

Edit

This method could obviously be applied with any number of k/v stores, I've used Redis here as a concrete example since I believe it meets the requirements closely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文