如何避免 GoogleAppEngine DataStore 延迟?
我是 App Engine 的新手,编写了一个示例应用程序。如果我创建或更新实体:
Entity record = new Entity(...);
... set properties
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(record);
然后重定向到显示新实体或更新实体的页面
resp.sendRedirect("MainPage.jsp");
,其中执行以下代码
DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
Query query = new Query(...).addSort(..., Query.SortDirection.DESCENDING);
List<Entity> entities = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
新记录不在列表中。页面已更新(如显示的时间戳所示),但只有在刷新页面时延迟最多几秒后才会显示新记录或现有记录的修改。
如何避免这种情况? DataStore 可能不适合这样的事情吗?
我正在使用 GAE 的 Eclipse 本地测试环境和 Windows XP 64。
I am new to App Engine and wrote a sample App. If i create or update a entity:
Entity record = new Entity(...);
... set properties
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
datastore.put(record);
and then redirect to a page where the new or updated entity is displayed
resp.sendRedirect("MainPage.jsp");
where the following code is executed
DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
Query query = new Query(...).addSort(..., Query.SortDirection.DESCENDING);
List<Entity> entities = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));
The new record is not in the list. The page is updated (as shown by a timestamp displayed), but the new record or a modification of a existing record is shown only after a delay of up to some seconds when i refresh the page.
How can this be avoided?
Is the DataStore possible not suited for such a thing?
I am using the Eclipse local test environment from GAE with Windows XP 64.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

是的,这称为“最终一致性”,是 HRD 的默认更新模型(现在所有新实例上的默认值)。
因此,放置一个实体然后查询它可能不会返回(事件一致性),但是放置它然后获取它(通过键获取)将立即返回(强一致性)。
解决方案是:
get
而不是查询。此外,本地开发服务器仅用于测试,其行为与生产实例并不完全相同,因此您应该始终(最终)在生产上进行测试。
Yeah, this is called "eventual consistency" and is the default update model for HRD (now default on all new instances).
So putting an entity and then querying for it might not get is back (evental consistency), but putting it and then getting it (
get
via a key) will get it back immediately (strong consistency).Solution is either to:
get
instead of queries.Also, local dev server is only for testing and does not behave exactly as production instances, so you should always (eventually) test on production.