[英]Google App Engine counters
For all my data in the GAE Datastore I have a model for keeping track of counters/total number of records (since we can't use traditional SUM queries). 对于GAE数据存储区中的所有数据,我有一个用于跟踪计数器/记录总数的模型(因为我们不能使用传统的SUM查询)。 I want to know the most efficient way of incrementing these global count values whenever I insert/delete a record.
每当我插入/删除记录时,我想知道增加这些全局计数值的最有效方法。 This is what I'm currently doing:
这就是我目前正在做的事情:
counter = DBCounter.all().fetch(1)
dbc = DBCounter(totalTopics=counter[0].totalTopics+1)
dbc.put()
But this seems quite sloppy to me. 但这对我来说似乎很草率。 Any thoughts on a better way to do this?
有没有想过更好的方法呢?
There are a few issues with your approach: 您的方法存在一些问题:
Here are some alternate approaches (from least accurate [and fastest] to most accurate [and slowest]): 以下是一些替代方法(从最不准确[和最快]到最准确[和最慢]):
If you need to keep scalability while counting, you should look into Joe Gregorio's article on sharding counters and DocSavage's implementation of the idea. 如果你需要在计算时保持可扩展性,你应该看看Joe Gregorio关于分片计数器和DocSavage 实现这个想法的文章。
AppEngineFan's excellent blog also has info on scalable non-sharded counters, see this one which uses task queues and points to the previous article on using cron jobs instead. AppEngineFan的优秀博客也提供有关可扩展非分片计数器的信息,请参阅此一个使用任务队列并指向上一篇关于使用cron作业的文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.