英文:
Google App Engine Go score counting and saving
问题
我想用Go语言制作一个简单的GAE应用程序,让用户投票并以两种方式存储他们的答案。第一种方式是原始数据(“投票给X”),第二种方式是这些投票的计数(“X有12票,Y有10票”)。在多个人同时访问应用程序的情况下,有什么有效的方法可以存储这两个值?如果我从Datastore中检索数据,对其进行更改,并为一个实例保存回去,那么另一个实例可能会同时进行相同的操作,我不确定最终结果是否正确。
英文:
I want to make a simple GAE app in Go that will let users vote and store their answers in two ways. First way will be raw data (Database store of "voted for X"), the second will be a running count of those votes ("12 votes for X, 10 votes for Y"). What is an effective way to store both of those values with the app being accessed by multiple people at the same time? If I retrieve the data from the Datastore, change it, and save it back for one instance, another might be wanting to do the same in parallel, and I`m not sure if the final result will be correct.
答案1
得分: 1
似乎一个好的方法是将所有的投票事件作为单独的实体存储(即“投票给X”的方式),并使用任务队列进行重新计算(即“X有12票,Y有10票”的方式),这样重新计算可以离线和顺序地进行(没有任何竞争和其他并发问题)。然后,您需要定期将重新计算任务放入队列中,以便更新结果。
任务队列不允许添加与现有任务同名的另一个任务,但也不允许检查特定任务是否已经入队,所以可能只需尝试将同名任务添加到队列中,就足以确保多个重新计算任务不存在。
另一种方法是使用goroutine等待来自输入通道的触发,以便重新计算结果。我在App Engine上还没有运行过这样的goroutine,所以对这种方法的一般行为不确定。
英文:
It seems like a good way to do that is to simply store all vote events as separate entities (the "voted for X" way) and use the Task Queue for the recalculation (the "12 votes for X, 10 votes for Y" way), so the recalculation is done offline and sequentially (without any races and other concurrency issues). Then you'd have to put the recalc task every once in a while to the queue so the results are updated.
The Task Queue doesn't allow adding another task with the same name as an existing one, but doesn't allow checking whether a specific task is already enqueued, so maybe simply trying adding a task with a same name to the queue will be enough to be sure that multiple recalc tasks are not there.
Another way would be to use a goroutine waiting for a poke from an input channel in order to recalculate the results. I haven't run such goroutines on App Engine so I'm not sure of the general behavior of this approach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论