如何正确处理 GAE 数据存储中的计数器更新?

发布于 2024-12-10 12:44:59 字数 136 浏览 6 评论 0原文

假设我的数据存储中有页面浏览量计数器。 为了在每个页面显示时增加其值,我需要读取该值,增加该值并增加。我如何确保当 2 个(或更多)用户查看该页面时,两个视图都会被计数(即,在更新 user1 的计数器值之前,不会出现 user2 的计数器值被读取的情况)?

Let's say I have page views counter in my datastore.
In order to increase its value with each page display, I need to read the value, increment that and increase. How can I be sure that in case when 2 (or more) users view the page, both views are counted (i.e. there will not be a case that for user2 we read the counter value before we've updated that for user1)?

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

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

发布评论

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

评论(2

醉酒的小男人 2024-12-17 12:44:59

对于此类情况,您需要使用分片计数器

You'll want to use Sharded Counters for such cases.

热风软妹 2024-12-17 12:44:59

作为精确值不如性能重要的分片计数器的替代方案,请考虑使用 后写式计数器。或者,使用 Google Analytics 之类的工具,而不是尝试自己计算页面浏览量。

As an alternative to sharded counters where exact values are less important than performance, consider using write-behind counters. Alternately, use something like Google Analytics, rather than trying to count pageviews yourself.

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