如何在Cloud Firestore中优化读写操作? [英] How to optimize read-write operations in cloud Firestore?

查看:64
本文介绍了如何在Cloud Firestore中优化读写操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个React + Firebase项目以供学习,我想知道应该采用哪种方法来有效地从Firebase中读取内容.

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.

说我有这个称为产品的只读集合,其中包含大约5000个文档,因此当用户访问我的react应用程序时,每次访问将为它收取5000次读取的费用.

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.

来源: Cloud Firestore:如何计算读数?

Source : Cloud Firestore: How is read calculated?

由于如果用户垃圾邮件刷新以响应应用程序,这将很快消耗读取计数,那么是否有任何正确方式从Firebase 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 ?

  1. 将产品信息存储在本地存储中

  1. Store product information in localstorage

  • 一旦React App成功加载数据,请继续将产品信息保存到本地存储中,以免将来不必要的加载.

使用Firebase中的SOURCE.CACHE

use SOURCE.CACHE from firebase

限制读取查询?

  • 每次加载都限制有固定数量的文档返回,但是到了最后,我仍然必须加载完整的文档集,因此我对此表示怀疑.

到目前为止,这是我能想到的,请告诉我您的应用程序构建设计中是否存在任何黄金标准或程序.

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.

谢谢.

推荐答案

您绝对应该引入分页策略.另一个聪明的方法是根据最后的变异时间进行查询.如果配置了 enablePersistence (否则将其设置为false),Firestore会自动将数据缓存在Web中.

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).

您可能会引入一种策略,即在经过一定时间后仅使用网络进行查询.不过,您需要在进行最后一次在线查询时跟踪每个模块.

You might introduce a strategy to only query using the network after a certain period has passed. 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)
  }
}

这可以是您针对特定页面会话的所有 Firestore 操作的实用程序功能.现在,您想要做的就是传递一个唯一的标识符,以及您要执行的所有离线或在线操作;可能是获取,插入,更新或删除;具有功能.您将确保该功能根据上次读写时间在离线或在线模式下执行.

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; 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.

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

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

这篇关于如何在Cloud Firestore中优化读写操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆