简体   繁体   English

Firestore 读取和缓存

[英]Firestore reads and cache

I'm trying to understand more about the reading from the server and reading from the cache and what is billable.我试图了解更多关于从服务器读取和从缓存读取以及收费的内容。

In this answer to a similar question Alex states:在这个对类似问题的回答中,亚历克斯说:

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.发生这种情况是因为您第二次执行查询时,结果来自缓存。

How do we know that the second query (identical to the first) is coming from the cache when the snapshot.metadata.isFromCache field is still false ?snapshot.metadata.isFromCache字段仍然为false时,我们如何知道第二个查询(与第一个相同)来自缓存? Another from Alex states similar thoughts here .另一个来自 Alex 的人在这里陈述了类似的想法。

According to Doug in comments here he states:根据道格在这里的评论,他说:

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.底线是:除非您处于离线状态或明确查询缓存,否则您需要为来自服务器的所有结果付费。

These seem to be opposing views.这些似乎是对立的观点。 I understand I can explicitly query the cache, that's not in question.我知道我可以明确查询缓存,这不是问题。 I'm just trying to understand 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?我只是想了解如果我运行相同的查询 10 次并且每次都返回 10 个文档(类似于 Alex 的示例/评论),我是按 100 次读取还是 10 次付费? (assuming the.network is available the whole time). (假设.network 一直可用)。

Any thoughts?有什么想法吗? Thanks in advance.提前致谢。

I wrote an article called:我写了一篇文章叫:

How to drastically reduce the number of reads when no documents are changed in Firestore? 如何在 Firestore 中没有文档更改时大幅减少读取次数?

In which I have explained everything regarding Firestore billing.我在其中解释了有关 Firestore 计费的所有内容。 I have also added a workaround on how to reduce the number of reads when there are no documents changed in the database.我还添加了一个变通方法,用于在数据库中没有更改文档时如何减少读取次数。

And to answer your questions:并回答你的问题:

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 as long as the (real-time) listener stays active.只要(实时)侦听器保持活动状态,就会发生这种情况。

Regarding the second answer, if you are listening for real-time changes and you perform a second query, within the 30 min limit, the results will always come from the cache.关于第二个答案,如果您正在侦听实时更改并执行第二次查询,则在 30 分钟的限制内,结果将始终来自缓存。 However, if you're only using a get() call, then each time you perform such an operation, you'll be billed as in the case of performing a brand new query.但是,如果您只使用 get() 调用,那么每次执行此类操作时,您都需要像执行全新查询一样付费。

When you are offline, and as long as you have the offline persistence enable, which is by default enabled in Firestore, you'll always read the data from the cache.当您处于离线状态时,只要启用了离线持久性(默认情况下在 Firestore 中启用),您就会始终从缓存中读取数据。 Cache reads have no cost.缓存读取没有成本。

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?如果我运行相同的查询 10 次并且每次都返回 10 个文档(类似于 Alex 的示例/评论),我是按 100 次读取还是 10 次付费?

If you're using a get() call, you'll be charged with 100 reads, but if you're listening for real-time updates and the listener isn't disconnected for 30 minutes, then you'll have to pay only 10 reads.如果您使用的是 get() 调用,您将被收取 100 次读取费用,但如果您正在监听实时更新并且监听器在 30 分钟内没有断开连接,那么您只需支付费用10 次阅读。

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

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