是否可以仅将新文档添加到Cloud Firestore缓存? [英] Is it possible to add only new documents to Cloud Firestore cache?

查看:73
本文介绍了是否可以仅将新文档添加到Cloud Firestore缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序每天读取5万至6万个文档,而我现在负担不起其他计划,因此我正在寻找一种优化读取的方法,以尽可能减少读取.

I have an app that has 50k - 60k document reads a day and I can't afford another plan now, so I'm looking for a way to optimize reads, getting less reads possible I can.

该数据库有4个集合以及一些子集合,我认为整个服务器中可能有大约1000个文档.

The database has 4 collections with some subcollections, I think I might have around 1000 documents in the whole server.

仍然存在:我可以从Firestore云中读取文档(用户首次打开应用程序)并将其存储在Firestore缓存中,然后,使该应用程序仅从Firestore缓存中加载数据,如果有新的文档,它仅读取这些文档,存储到缓存中,并继续从缓存中读取内容?

It is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache, after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, store to the cache, and keep reading from the cache?

如果可能的话,该应用只能从缓存中加载数据(使用参数"source"),而永远不能直接从Firestore中加载数据.

The app could only load data from cache (using the parameter "source") and never directly from Firestore, if possible.

Firestore.instance.collection("images").getDocuments(source: Source.cache);

推荐答案

仍然存在:我可以从Cloud Firestore中读取文档(用户首次打开应用程序时)并将其存储在Firestore缓存中

Is is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache

这是默认行为.根据有关 Firestore离线持久性的官方文档:

That's the default behavior. According to the official documentation regarding Firestore offline persistence:

  • 对于Android和iOS,默认情况下启用离线持久性.要禁用持久性,请将PersistenceEnabled选项设置为false.

  • For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

对于Web,默认情况下禁用脱机持久性.要启用持久性,请调用enablePersistence方法.会话之间不会自动清除Cloud Firestore的缓存.因此,如果您的Web应用程序处理了敏感信息,请确保在启用持久性之前先询问用户是否在受信任的设备上.

For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.

因此,您不需要执行任何特殊操作.在其中一个集合上打开流后,数据将添加到缓存中.

So there is nothing special that you need to do. Once you open a stream on one of your collections, the data is added to the cache.

之后,让该应用仅从Firestore缓存中加载数据,如果有新文档,它仅读取这些文档,存储到缓存中,并继续从缓存中读取内容?

after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, stores to the cache, and keeps reading from the cache?

这仍然是默认行为,但是只要不更改数据库中的文档,此方法就可以工作.如果数据库中的文档发生了更改,您将需要为每个文档读取一个文档.机制是相同的.

That's again the default behavior, but this work as long as the documents in your database are not changed. If a document in the database is changed, you'll be charged with a document read for each document. The mechanism is the same.

除此之外,您可以告诉Firestore仅在需要时从缓存中读取数据,如以下帖子中的答案所述:

Besides that, you can tell Firestore to read data only from the cache if needed, as explained in the answer from the following post:

这篇关于是否可以仅将新文档添加到Cloud Firestore缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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