[英]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 读取数据?
Store product information in localstorage将产品信息存储在localstorage
use SOURCE.CACHE from firebase使用来自 firebase 的 SOURCE.CACHE
limit read query?限制读取查询?
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.