【发布时间】:2018-07-18 03:40:28
【问题描述】:
我正在使用 Firebase 和 Cloud 功能来测试存储在 Firebase 实时数据库中的一百万个预先生成的代码的良好价值。 它将在移动应用程序中用于验证用户是否在现实生活中购买了捆绑包。
我找到了 2 个可行的解决方案。首先,我将代码直接放在属性的名称中。在第二个中,我将代码放在名为“key”的子属性中 第二种情况,关键参数是indexed。
我需要快速(log n 复杂度)访问来获得响应。
您知道我的任何解决方案是否可以在 Firebase 上每秒处理大约 100 万个条目和 100 个调用。
(我不熟悉 NoSQL。)
在我的示例中,代码是“ABCD-0000-000X” (不要考虑称为“用户”的属性)
第一个解决方案:使用代码值作为父级
云函数源代码
exports.checkKey = functions.https.onRequest((req, res) => {
const code = req.query.code;
return admin.database().ref("Codes/" + code).once("value").then(snapshot => {
if (snapshot.val() === null) {
return res.send("Invalid Code");
}
const nb = snapshot.child("nb");
if (nb.val() > 4) {
return res.send("NO more code");
}
snapshot.ref.update({ "nb": nb.val() + 1 });
return res.send("OK");
});
第二种解决方案:使用子代码
exports.getKey = functions.https.onRequest((req, res) => {
const code = req.query.code;
var ref = admin.database().ref("Codes");
ref.orderByChild("key").equalTo(code).on("child_added", function (snapshot) {
const nb = snapshot.child("nb");
if (nb.val() > 4) {
return res.send("NOK");
}
snapshot.child("nb").set(nb.val() + 1);
return res.send("OK");
}
});
感谢您的帮助。
【问题讨论】:
-
在尝试提出更多问题之前,请阅读What types of questions should I avoid asking?。
-
"你能帮我理解这两种情况会发生什么吗" 你不明白什么? “每秒大约 100 万个条目和 100 个调用的最佳解决方案是什么。”征求意见的问题被视为离题。
-
我编辑了我的问题。我有 2 个解决方案,我只想帮助找到适合我的问题的解决方案。这对我很重要。
标签: javascript firebase firebase-realtime-database google-cloud-functions