在Google App Engine中,如何处理表单提交的最终一致性? [英] In Google App Engine, how can I work with eventual consistency in handling form submissions?

本文介绍了在Google App Engine中,如何处理表单提交的最终一致性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,通过最终一致性,常用的表单处理工作流(提交 - >创建/更新记录 - >重定向 - >重新加载)不起作用。重定向后,新记录(可能)将不可用于显示。我应该如何处理表单,以便在重新加载时显示更新?



我可以尝试使用强一致性,但作为 App Engine文档注意到,更新仅限于一个更新每秒



那么我该如何处理一个能够提供最终一致性的即时用户反馈的表单?



我有一个用户仪表板,用户可以创建并删除项目。我的实体看起来像这样:

  class User(ndb.Model)
...

class Item(ndb.Model)
user = ndb.KeyProperty(User,required = True)

过去,我会在响应用户仪表板的GET请求时执行这样的查询。

  items = Item.query(user = user.key)

这是一个糟糕的体验,因为用户会删除一个项目,并且在POST / redirect / GET后,由于最终的一致性,刚删除的项目会再次出现在仪表板中。为了解决这个问题,我改变了我的用户实体有一个像这样的项目列表:

  class User(ndb.Model)
items = ndb.KeyProperty(重复= True)
...

现在,当我显示仪表板时,这:

  items = ndb.get_multi(user.items)

由于我现在正在通过密钥获取,d ata始终是最新的。



这对我来说很有用,因为用户不会有那么多项目。但是,如果用户可能拥有数千个项目,则由于实体大小限制,此方法无效。


I've noticed that, with eventual consistency, the common form-processing workflow that I am used to (submit -> create/update record -> redirect -> reload) doesn't work. Upon redirect, the new record (probably) won't be available for display. How should I handle forms so that updates are displayed upon reload?

I could try to use strong consistency, but as the App Engine documentation notes, updates are limited to one update per second.

So how can I process a form providing immediate user feedback with eventual consistency?

解决方案

Try to restructure your code so that you are getting by key (which always gives you the most recent data) instead of doing a query. I realize this isn't always possible, but I'll give you a recent example of something that worked for me.

I have a user dashboard where a user can create and delete "items". My entities looked like this:

class User(ndb.Model)
    ...

class Item(ndb.Model)
    user = ndb.KeyProperty(User, required=True)

In the past, I would do a query like this when responding to a GET request for the user dashboard.

items = Item.query(user=user.key)

This was a bad experience because a user would delete an item and after the POST/redirect/GET the just deleted item would again appear in the dashboard because of eventual consistency.

To fix this, I changed my User entity to have a list of Items like this:

class User(ndb.Model)
    items = ndb.KeyProperty(repeated=True)
    ...

Now, when I show the dashboard, I do this:

items = ndb.get_multi(user.items)

Since I am now getting by key, the data is always up to date.

This works for me because a user won't have that many items. If a user could have thousands of items, however, this approach wouldn't work because of the entity size limit.

这篇关于在Google App Engine中,如何处理表单提交的最终一致性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆