在 Google AppEngine 中使用分片计数器存储每小时的点击次数
我正在为谷歌应用程序引擎开发一个Python应用程序,它使用分片计数器来计算不同类型的点击。
我的问题是我想获得按小时划分的点击次数统计数据,而不仅仅是所有点击次数的总和。
实现这一目标的一种方法是向分片索引添加时间戳:
def txn():
index = random.randint(0, config.num_shards - 1)
shard_name = code + str(index) # + timestamp without seconds
counter = ClickCounter.get_by_key_name(shard_name)
if counter is None:
counter = ClickCounter(key_name=shard_name, code=code)
counter.click += 1
counter.put()
db.run_in_transaction(txn)
问题是计算一个月内的所有分片会慢 700 倍以上。
有没有好的方法来缓存结果?我的意思是,一旦过了一个小时,计数器就不会再改变了。将每次点击保存在新对象中是否有缺点?
I'm developing an app in python for google app engine that count different kind of clicks with sharded counters.
My problem is that I want to get statistics split by hour from the clicks and not just a total sum of all clicks.
One way to achieve that is to add a timestamp to the index of the shards:
def txn():
index = random.randint(0, config.num_shards - 1)
shard_name = code + str(index) # + timestamp without seconds
counter = ClickCounter.get_by_key_name(shard_name)
if counter is None:
counter = ClickCounter(key_name=shard_name, code=code)
counter.click += 1
counter.put()
db.run_in_transaction(txn)
The problem would be that counting all the shards for a month would be more than 700 times slower.
Is there a good way to cache the results? I mean, once an hour has passed the counter won't change any more. Is there a drawback with saving every click in a new object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案将会起作用 - 只需使用任务队列将您的分片记录聚合成漂亮的、易于报告的摘要记录即可。
Your solution will work - just use the task queue to aggregate your sharded records into nice, reporting-friendly, summary records as you go.