firebase - 文本注释应用程序的 Cloud Firestore 架构
问题描述
我有一个用于阅读和注释文本的应用程序,想知道如何最好地构建底层 Firestore 数据库。
应用程序本身是一个在浏览器中运行的相对简单的 ReactJS SPA。所有用户都可以独立地将文本文档上传到系统中,然后在用户界面中对这些文档进行注释。要进行注释,用户打开文档,单击一个单词,然后在弹出窗口中输入有关该单词的一些元数据。然后,系统会突出显示该用户的所有文档中该词的每一次出现,并根据所提供的元数据使用不同的颜色。
我最初的计划是创建 2 个独立的集合:
- 一个集合/documents,其中包含用户上传的每个文本文档的一个 Firestore 文档。我们预计每位用户平均拥有 200 个文本文档,每个文档包含多达 200kb 的数据并引用多达 1.000 个带注释的单词。
- 一个集合/words,其中包含用户注释的每个单词的一个 Firestore 文档。我们预计每个用户平均有 30.000 个注释,每个注释大约有 500 字节的数据。
我现在有点担心这样的数据库方案会带来相对较高的运营成本,因为每次显示文本文档时都必须从数据库中加载相关的注释(而 Firestore Blaze 计划将向我收取 0.06 美元的费用) 100.000 次读取)。
是否有更好(更具成本效益)的方式来构建这个数据库?
解决方案
您应该仅通过仅检索文档的最新版本来获取已更改的数据并使用缓存。如果没有任何变化,这将只导致一次读取操作。通常,该get()
调用仅检索文档的最新快照并忽略脱机缓存。但是,如果网络不可用或请求超时,您可以利用离线缓存。
var docRef = db.collection("cities").doc("SF");
// Valid options for source are 'server', 'cache', or
// 'default'. See
// https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions
// for more information.
var getOptions = {
source: 'cache'
};
// Get a document, forcing the SDK to fetch from the offline cache.
docRef.get(getOptions).then(function(doc) {
// Document was found in the cache. If no cached document exists,
// an error will be returned to the 'catch' block below.
console.log("Cached document data:", doc.data());
}).catch(function(error) {
console.log("Error getting cached document:", error);
});
推荐阅读
- mysql - MySQL 事件调度程序
- reactjs - Redux 失败的道具类型:无法将类作为函数调用
- java - 使用 Java 更新 Excel 文件(插入行)
- html - 导航栏中心问题
- mysql - 将 json_encode 字符串保存到 mysql db
- python - 运行升级时 Alembic 显示“空消息”
- javascript - $http 成功功能块中不可用的动态元素和范围变量
- python - 如何使用 selenium chrome 驱动程序在 Python 中按名称过滤网络调用条目?
- tensorflow - 使用 python mnist_tpu.py --use_tpu=false --tpu='' 在 TPU 中出现错误
- node.js - 函数内的nodejs回调函数->如何返回结果