简体   繁体   English

Google Cloud端点与Android - 缓存响应?

[英]Google Cloud Endpoints with Android - Caching response?

I am using Google Cloud Endpoints (Java) as a backend for an Android app. 我使用Google Cloud Endpoints (Java)作为Android应用的后端。 The data on the backend is stored to Datastore . 后端上的数据存储在数据存储区中

The problem is that the Android client often doesn't receive correct data from the backend. 问题是Android客户端通常无法从后端接收正确的数据。 After repeating the request, the correct data is eventually received. 重复请求后,最终会收到正确的数据。

Can you tell me what could cause this problem ? 你能告诉我什么可能导致这个问题吗? Is there some kind of response caching in the Cloud Endpoints library ? Cloud Endpoints库中是否存在某种响应缓存? Or caching within Datastore ? 或者在Datastore中缓存?

I have read about eventual consistency provided by Datastore, so I am trying to wait at least 10 seconds after updating the entities before the next request with query. 我已经阅读过Datastore提供的最终一致性 ,因此我尝试在下一次查询请求之前更新实体后至少等待10秒。 Is it enough ? 够了吗?

Thanks a lot. 非常感谢。

Straight from the docs at Structuring Data for Strong Consistency : If you always require the result of a Datastore query to be consistent, you will need to use an ancestor query as per the example code in the article: 直接从构造数据以获得强一致性的文档:如果始终要求数据存储区查询的结果保持一致,则需要根据文章中的示例代码使用祖先查询:

Query query = new Query("Greeting", guestbookKey).setAncestor(guestbookKey);

You can't rely on a delay to obtain consistency as it depends on replication across data centers and the amount of time this takes is never, well, consistent :) 您不能依赖延迟来获得一致性,因为它依赖于跨数据中心的复制,并且这需要的时间永远不会是一致的:)

It sounds as though the way you structured your data model is not compliant with the way in which you want to use the data model. 听起来好像您构建数据模型的方式不符合您希望使用数据模型的方式。 Without seeing an example of your data model I can't give you hints as to how to change it but here is a simple example that might help you. 如果没有看到您的数据模型的示例,我无法向您提供有关如何更改它的提示,但这是一个可能对您有所帮助的简单示例。

Suppose I have a "Post" entity and a "CommentForPost" entity. 假设我有一个“Post”实体和一个“CommentForPost”实体。 Users can create many CommentForPost entries for each Post entry. 用户可以为每个Post条目创建许多CommentForPost条目。 Logically, the CommentForPost is a child of Post but if you don't make the Post an ancestor of all the CommentForPost entries then immediate consistency is not guaranteed. 从逻辑上讲,CommentForPost是Post的子项,但是如果不使Post成为所有CommentForPost条目的祖先,则无法保证立即一致性。 If user A creates a comment and then queries for it they may not see the entry right away. 如果用户A创建了评论然后查询,他们可能不会立即看到该条目。

On the other hand, if you make the Post entity the parent of all of the CommentForPost entries then you will guarantee immediate consistency because they are being saved as a single EntityGroup. 另一方面,如果将Post实体作为所有CommentForPost条目的父级,那么您将保证立即一致性,因为它们被保存为单个EntityGroup。 If user A creates an entry and immediately queries for it (using the Post key as an ancestor) then they are for sure going to get accurate data back. 如果用户A创建了一个条目并立即查询它(使用Post键作为祖先),那么他们肯定会得到准确的数据。

The limitation here is that you would only be able to create one CommentForPost entry per second. 这里的限制是您每秒只能创建一个CommentForPost条目。 This is where the give and take lays. 这是给予和接受的地方。 If you need a lot of updates within short periods of time then you can't guarantee consistency (eg 100 users are all adding CommentForPost entries at the same time). 如果您需要在短时间内进行大量更新,则无法保证一致性(例如,100个用户同时添加了CommentForPost条目)。 If you want to guarantee consistency then you can't have a lot of updates within a short period of time. 如果您想保证一致性,那么您将无法在短时间内获得大量更新。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM