简体   繁体   English

如何优化 Cloud Firestore 中的读写操作?

[英]How to optimize read-write operations in cloud Firestore?

I am currently writing a react + Firebase project for learning purpose, and I am wondering which approach should I take to read from firebase efficiently.我目前正在编写一个用于学习目的的 react + Firebase 项目,我想知道我应该采用哪种方法来有效地读取 firebase。

Say I have this read only collection called product where it contains around 5000 documents hence when user access my react app then it would be charged for 5000 reads per access.假设我有一个名为 product 的只读集合,其中包含大约 5000 个文档,因此当用户访问我的 React 应用程序时,每次访问将收取 5000 次读取费用。

Source: Cloud Firestore: How is read calculated?资料来源: Cloud Firestore:读取是如何计算的?

Since this would consume the read counts rather quickly if user spams refresh to react app, is there any proper way to read the data from firebase firestore?由于如果用户垃圾邮件刷新以响应应用程序,这将很快消耗读取计数,是否有任何正确的方法从 firebase firestore 读取数据?

  1. Store product information in localstorage将产品信息存储在localstorage

    • Once react app successfully loads the data, proceed to save product information into localstorage to avoid unnessary loading in the future.一旦 React 应用程序成功加载数据,继续将产品信息保存到本地存储中,以避免将来不必要的加载。
  2. use SOURCE.CACHE from firebase使用来自 firebase 的 SOURCE.CACHE

  3. limit read query?限制读取查询?

    • Limits a fixed amount of documents return of each load, but at the end of the day I will still have to load the full set of documents hence I am quite skeptical of this.限制每次加载的固定数量的文档返回,但在一天结束时我仍然必须加载完整的文档集,因此我对此持怀疑态度。

This is what I can think of so far, please kindly let me know if there is any golden standard or procedure in the your app building design.这是我目前能想到的,如果您的应用程序构建设计中有任何黄金标准或程序,请告诉我。

Thank you.谢谢你。

You should definitely introduce a pagination strategy.您绝对应该引入分页策略。 Another smart way is to query based on last mutation time.另一种聪明的方法是根据上次突变时间进行查询。 Firestore automatically caches your data in the web if enablePersistence is configured(Otherwise its set to false).如果配置了enablePersistence (否则将其设置为 false),Firestore 会自动将您的数据缓存在 web 中。

You might introduce a strategy to only query using the.network after a certain period has passed.您可能会引入一种策略,仅在经过一段时间后使用 .network 进行查询。 You need to keep track per module when the last online query was made though.但是,您需要在进行最后一次在线查询时跟踪每个模块。

function strategicFirestoreReadWrite(moduleKey, actionFn) {
  const lastFetchedDate = sessionStorage.getItem(moduleKey) || new Date();
  const difference = Math.abs(lastFetchedDate.getTime() - new Date().getTime())
  const hourDifference = difference  / 1000 / 3600
  const logToStorageFn = () => {
    sessionStorage.setItem(moduleKey, new Date())
  }

  // Performing operation offline when last fetch earlier than 3 hours
  if (hourDifference < 3) {
   firebase
     .firestore()
     .disableNetwork()
     .then(actionFn)
     .then(logToStorageFn)
  } else {
   firebase
       .firestore()
       .enableNetwork()
       .then(actionFn)
       .then(logToStorageFn)
  }
}

This can be a utility function for all your Firestore operations for a specific page session. Now what you wanna do is pass a unique identifier and whatever offline or online action you want to perform;这可以是一个实用程序 function,用于特定页面 session 的所有Firestore操作。现在您要做的是传递一个唯一标识符以及您要执行的任何离线或在线操作; maybe a fetch, insert, update or delete;可能是获取、插入、更新或删除; with a function. You will be sure that the function performs in either in offline or online mode based on last read-write time.使用 function。您将确定 function 根据上次读写时间在离线或在线模式下执行。

strategicFirestoreReadWrite(window.location.href, () => {

   //whatever operation you want to perform in online or offline mode
})

You listed quite a few good options.你列出了很多不错的选择。 We use pagination in our app to limit the read queries and also to improve the load speed.我们在我们的应用程序中使用分页来限制读取查询并提高加载速度。 For example, we only show 50 items per page and load more on click or scroll (infinite scroll) If the data doesn't change often, you can cache it on the frontend, but this introduces a whole lot of additional problems:).例如,我们每页只显示 50 个项目,并在点击或滚动时加载更多(无限滚动)如果数据不经常更改,您可以将其缓存在前端,但这会引入很多额外的问题:)。

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

相关问题 优化NodeJs中stream读写操作 - Optimize stream read-write operation in NodeJs 如何给google function读写权限? - How to give to google function read-write permissions? Flutter Firestore - 如何读写对象的 Arrays - Flutter Firestore - How To Read And Write Arrays of Objects 使用 Cloud Functions 执行 Firestore 操作是否更安全? - Is it safer to do Firestore operations using Cloud Functions? Firestore Cloud Function - 如何从 url 读取参数 - Firestore Cloud Function - How to read params from url 如何从云 function 对 firestore 数据库进行 CRUD 操作? (节点.js) - How to do CRUD operations on a firestore DB from a cloud function? (Node.js) 如何在 firebase firestore 设置规则中授予读写权限 - How can give access to read but not write in firebase firestore settting rules 如何使用复杂的 JSON Webhook 中的 Node JS Cloud Function 写入 Cloud Firestore? - How to write to the Cloud Firestore using Node JS Cloud Function from Complex JSON Webhook? 如何读取和写入谷歌云数据存储? - How do I read and write to the google cloud datastore? Cloud Function 无法解析 JSON 并从 Webhook 写入 Cloud Firestore - Cloud Function failing to parse JSON and write to Cloud Firestore from Webhook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM