当谈到使用官方 web SDK 的 Firestore 缓存时,它是否优化了读取以便仅当文档在上次读取后发生更改时才向服务器“发送”读取请求? (因此每次尝试不会生成 1 次额外的文档读取)
为了详细说明,请考虑以下场景:
- 用户在网络应用上打开他们的个人资料页面
- Firestore 向服务器发出请求以获取配置文件文档(1 次读取定价)
- 用户离开页面并在一段时间后返回个人资料
- 网络应用需要再次访问配置文件
在第 4 步中,Firestore 是否会默认从服务器请求文档,即使它没有更改? (这意味着每次用户导航到个人资料页面时,它将计为 1 次额外阅读)
如果是,是否可以配置 Firestore,使其仅使用缓存的对象并仅在服务器上的对象更改时更新它?如何做到这一点?
最佳答案
默认情况下,Firestore SDK disables local persistence of cached data for web clients (但默认为 Android 和 iOS 启用)。如果要启用持久性,请按照 documentation 中的说明进行操作。
firebase.firestore().enablePersistence()
.catch(function(err) {
if (err.code == 'failed-precondition') {
// Multiple tabs open, persistence can only be enabled
// in one tab at a a time.
// ...
} else if (err.code == 'unimplemented') {
// The current browser does not support all of the
// features required to enable persistence
// ...
}
});
// Subsequent queries will use persistence, if it was enabled successfully
如果文档在本地缓存中是最新的,则不会向您收取阅读文档的费用。
关于javascript - 网络应用 : Firestore cache - read changed documents only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629892/