问题描述
假设我有一个运行Firebase Firestore数据库的网络/移动应用程序。
Let's say i have a web/mobile app running firebase firestore database.
我的应用设置为主要提供动态内容,这些内容完全存储在firestore中。
My app is set to serve mostly dynamic content fully stored in firestore.
所以我们在谈论缓存动态内容
So we are talking about caching dynamic content
如果用户加载页面A,它将发出1个请求储存(例如)。如果此用户转到页面B,然后在5分钟后返回页面A,则我希望应用程序在内容未更改的情况下再次发出请求。
If a user load Page A, it's will make 1 request to firestore (for example). If this user go to Page B and then go back to Page A 5 minutes later, i don't want the app to make another request if the content has not changed.
我听说过 http cache-control
标头,但我很担心。如果缓存控件转到检查存储区以了解内容是否相同,则存储区会将此操作计为请求吗?
I heard about http cache-control
headers but my concern is. If the cache control go check firestore to learn if the content is still the same, will this operation be counted as a request by firestore ?
推荐答案
Firestore不会以适用于HTTP Cache-Control标头的方式发出请求。
Firestore does not make requests in a way that HTTP Cache-Control headers would apply.
但是,包含一个恢复令牌的概念,该令牌允许以后重新查询整个查询,可以避免重新传输与查询匹配的所有未更改文档。
However, the protocol includes a notion of a resume token which allows entire queries to be resumed at a later time, possibly avoiding the retransmission of any unchanged documents that match the query.
如果您后,Firestore会为您缓存文档并在本地IndexedDB中恢复令牌。如果您稍后再返回页面,它将透明地使用恢复令牌,并避免重新传输。
If you enable persistence in your web app, Firestore will cache the documents and resume tokens in a local IndexedDB for you. If you come back to a page later, it will use the resume token transparently and avoid retransmission.
请注意:
因此,专门解决您的问题。如果:
So, to specifically address your question. If:
- 您已启用持久性,
- 基础数据未更改,
- 用户加载页面A,
- 5分钟后再次加载页面A
- you have persistence enabled,
- the underlying data hasn't changed,
- a user loads page A, and
- loads page A again 5 minutes later
然后Firestore将请求该页面的数据,但服务器将实质上响应为未发生任何更改:不会传输任何文档,也不会收取任何费用来确认。
Then Firestore will request the data for the page but the server will essentially respond that nothing has changed: no documents will be transferred and there will be no charge to confirm that.
这篇关于如何使用Http缓存控件保存Firestore请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!