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

时间:2022-08-03 23:08:15

I am using Google Cloud Endpoints (Java) as a backend for an Android app. The data on the backend is stored to Datastore.

我使用Google Cloud Endpoints(Java)作为Android应用的后端。后端上的数据存储在数据存储区中。

The problem is that the Android client often doesn't receive correct data from the backend. After repeating the request, the correct data is eventually received.

问题是Android客户端通常无法从后端接收正确的数据。重复请求后,最终会收到正确的数据。

Can you tell me what could cause this problem ? Is there some kind of response caching in the Cloud Endpoints library ? Or caching within Datastore ?

你能告诉我什么可能导致这个问题吗? Cloud Endpoints库中是否存在某种响应缓存?或者在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. Is it enough ?

我已经阅读过Datastore提供的最终一致性,所以我试图在下一次查询请求之前更新实体后至少等待10秒。够了吗?

Thanks a lot.

非常感谢。

2 个解决方案

#1


1  

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. Users can create many CommentForPost entries for each Post entry. 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. If user A creates a comment and then queries for it they may not see the entry right away.

假设我有一个“Post”实体和一个“CommentForPost”实体。用户可以为每个Post条目创建许多CommentForPost条目。从逻辑上讲,CommentForPost是Post的子项,但如果您不使Post成为所有CommentForPost条目的祖先,则无法保证立即一致性。如果用户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. 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.

另一方面,如果您将Post实体作为所有CommentForPost条目的父级,那么您将保证立即一致性,因为它们被保存为单个EntityGroup。如果用户A创建了一个条目并立即查询它(使用Post键作为祖先),那么他们肯定会得到准确的数据。

The limitation here is that you would only be able to create one CommentForPost entry per second. 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 (e.g. 100 users are all adding CommentForPost entries at the same time). If you want to guarantee consistency then you can't have a lot of updates within a short period of time.

这里的限制是你每秒只能创建一个CommentForPost条目。这是给予和接受的地方。如果您需要在短时间内进行大量更新,则无法保证一致性(例如,100个用户同时添加了CommentForPost条目)。如果您想保证一致性,那么您将无法在短时间内获得大量更新。

Make sense?

合理?

#2


2  

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 :)

您不能依赖延迟来获得一致性,因为它依赖于跨数据中心的复制,并且这需要的时间量永远不会一致:)

#1


1  

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. Users can create many CommentForPost entries for each Post entry. 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. If user A creates a comment and then queries for it they may not see the entry right away.

假设我有一个“Post”实体和一个“CommentForPost”实体。用户可以为每个Post条目创建许多CommentForPost条目。从逻辑上讲,CommentForPost是Post的子项,但如果您不使Post成为所有CommentForPost条目的祖先,则无法保证立即一致性。如果用户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. 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.

另一方面,如果您将Post实体作为所有CommentForPost条目的父级,那么您将保证立即一致性,因为它们被保存为单个EntityGroup。如果用户A创建了一个条目并立即查询它(使用Post键作为祖先),那么他们肯定会得到准确的数据。

The limitation here is that you would only be able to create one CommentForPost entry per second. 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 (e.g. 100 users are all adding CommentForPost entries at the same time). If you want to guarantee consistency then you can't have a lot of updates within a short period of time.

这里的限制是你每秒只能创建一个CommentForPost条目。这是给予和接受的地方。如果您需要在短时间内进行大量更新,则无法保证一致性(例如,100个用户同时添加了CommentForPost条目)。如果您想保证一致性,那么您将无法在短时间内获得大量更新。

Make sense?

合理?

#2


2  

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 :)

您不能依赖延迟来获得一致性,因为它依赖于跨数据中心的复制,并且这需要的时间量永远不会一致:)