【问题标题】:Web app: Firestore cache - read changed documents onlyWeb 应用程序:Firestore 缓存 - 仅读取更改的文档
【发布时间】:2019-07-04 21:47:37
【问题描述】:

当谈到使用官方 Web SDK 进行 Firestore 缓存时,它是否会优化读取,以便仅在上次读取后文档发生更改时才向服务器“发送”读取请求? (因此每次尝试不会生成 1 次额外的文档读取)

稍微详细一点,考虑以下场景:

  1. 用户在网络应用上打开他们的个人资料页面
  2. Firestore 向服务器发出请求以获取配置文件文档(根据定价读取 1 个)
  3. 用户离开页面并在一段时间后返回个人资料
  4. 网络应用需要再次访问配置文件

在第 4 步中,Firestore 是否会默认从服务器请求文档,即使它没有更改? (这意味着每次用户导航到个人资料页面时,它都将计入定价的 1 次额外阅读)

如果是,是否可以配置 Firestore,使其仅使用缓存的对象,并且仅在服务器上的对象更改时才更新它?该怎么做?

【问题讨论】:

  • FYI 反引号用于分隔问题中的代码位,而不是识别产品或强调术语。

标签: javascript firebase google-cloud-firestore


【解决方案1】:

默认情况下,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

如果文档在本地缓存中是最新的,则不会向您收取文档读取费用。

【讨论】:

  • 启用持久性后,有没有办法可靠地调试/分析流程?当我可以打开它并在应用程序中来回导航以查看它生成的读取次数时,可能类似于 RTDB 中的“配置文件”,或类似的东西?它似乎一直在产生读取,我正在寻找方法来了解正在发生的事情。
  • 我猜,无论如何,你都会被收取一次阅读费用(根据firebase.google.com/docs/firestore/pricing#minimum-charge)。
猜你喜欢
  • 2021-12-31
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
相关资源
最近更新 更多