在 App Engine 中使用后写计数器或分片计数器提交计数时,为什么找不到模型实例?

发布于 2024-12-02 07:28:50 字数 1103 浏览 1 评论 0原文

我正在开发 App Engine 应用程序的计数器,并遵循此处概述的示例 http://blog.notdot.net/2010/04/High-concurrency-counters-without-sharding

我无法理解以下几行的需要:

counter = cls.get_by_key_name(name)
if not counter:
  counter = cls()

似乎请求了增量为实例,然后当将该增量应用于数据存储时,找不到该实例并被替换为新实例。

分片计数器示例中使用了相同的模式 code.google.com/appengine/articles/sharding_counters.html

counter = SimpleCounterShard.get_by_key_name(shard_name)
    if counter is None:
        counter = SimpleCounterShard(key_name=shard_name)

在这种情况下,未找到的 key_name 与创建的 key_name 是一致的,因此当 counter. put() 最终被调用,似乎新的会覆盖未找到的。

我的用法与此示例类似,它基于上面的第一个示例: https: //github.com/pamelafox/ragetube/blob/master/models.py

我正在阅读第 66 行,因为“已观看了一首歌曲,是时候更新其观看次数了。获取歌曲,但如果你不能,创建一首新歌并给它这个计数”。那是不对的。

为什么 get_by_key_name() 无法返回记录?是否有一些默认行为使第一个示例起作用(即应该提供某种 key_name )?如果找不到实例,我只是从函数返回,我可能会遇到什么情况?

I'm working on a counter for an App Engine application and am following the example outlined here http://blog.notdot.net/2010/04/High-concurrency-counters-without-sharding

I'm having trouble understanding the need for the following lines:

counter = cls.get_by_key_name(name)
if not counter:
  counter = cls()

It seems an increment was requested for the instance, and then when that increment was to be applied to the datastore, the instance couldn't be found and was replaced with a new one.

The same pattern is used on the Sharding Counters example code.google.com/appengine/articles/sharding_counters.html

counter = SimpleCounterShard.get_by_key_name(shard_name)
    if counter is None:
        counter = SimpleCounterShard(key_name=shard_name)

In this case, the key_name is consistent between the one that wasn't found and the one that is created, so when counter.put() is eventually called it seems the new one will overwrite the not-found one.

My usage is similar to this example, which is based off the first example above: https://github.com/pamelafox/ragetube/blob/master/models.py

I'm reading line 66 as "a song was viewed and it's time to update its viewcount. Fetch the song, but if you can't, create a new song and give it this count". That can't be right.

Why would get_by_key_name() fail to return a record? Is there some default behavior that makes the first example work (ie, should a key_name of some kind be supplied)? What situation might I find myself in if I simply returned from the function if the instance couldn't be found?

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

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

发布评论

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

评论(1

陌生 2024-12-09 07:28:50

第一个应该是

counter = cls(key_name=name)

The first one should be

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