Firestore 读取和缓存

Firestore reads and cache

我想了解更多关于从服务器读取和从缓存读取以及收费的内容。

在对类似问题的回答中,Alex 指出:

However, if you perform a query, "again and again", as long as it's the same query and nothing has changed on the server, then you will not be charged with any read operations. This is happening because the second time you perform the query, the results are coming from the cache.

snapshot.metadata.isFromCache 字段仍然是 false 时,我们如何知道第二个查询(与第一个相同)来自缓存?另一个来自 Alex 的人表达了类似的想法 .

根据 Doug 在评论中的说法 他说:

The bottom line is this: unless you are offline or querying the cache explicitly, you are charged for all results that come from the server.

这些似乎是对立的观点。我知道我可以明确查询缓存,这不是问题。我只是想了解如果我 运行 相同的查询 10 次并且每次返回 10 个文档(类似于 Alex 的 example/comment),我会按 100 次读取还是 10 次付费? (假设网络一直可用)。

有什么想法吗?提前致谢。

我写了一篇文章叫:

How to drastically reduce the number of reads when no documents are changed in Firestore?

我在其中解释了有关 Firestore 计费的所有内容。我还添加了一个解决方法,用于在数据库中没有更改文档时如何减少读取次数。

并回答您的问题:

However, if you perform a query, "again and again", as long as it's the same query and nothing has changed on the server, then you will not be charged with any read operations.

只要 (real-time) 侦听器保持活动状态,就会发生这种情况。

关于第二个答案,如果您正在侦听 real-time 更改并执行第二个查询,则在 30 分钟限制内,结果将始终来自缓存。但是,如果您只使用 get() 调用,那么每次执行此类操作时,您都需要像执行全新查询一样付费。

当您处于离线状态时,只要启用离线持久性(默认情况下在 Firestore 中启用),您将始终从缓存中读取数据。缓存读取 成本。

if I run the same query 10 times and 10 documents are returned each time (similar to Alex's example/comment) will I be charged for 100 reads or 10?

如果您使用的是 get() 调用,您将按 100 次读取收费,但如果您正在监听 real-time 更新并且监听器在 30 分钟内没有断开连接,那么您只需支付 10 次阅读费用。