【问题标题】:What's the best way to check a list of 1 millions codes in Firebase/Cloud function? [closed]在 Firebase/Cloud 功能中检查 100 万个代码列表的最佳方法是什么? [关闭]
【发布时间】: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


【解决方案1】:

您无法查询一百万个项目的列表。因此,将密钥存储为名为 key 的属性是行不通的。

但是,如果您将密钥作为每个项目的密钥,则意味着您可以通过其路径访问该项目。而且它的扩展性非常好。

所以我会采用你的第一种方法。

也就是说:如果不了解所有用例,就很难推荐任何具体的东西,因为没有人(通常包括早期的项目创建者)可能知道。因此,我还建议您通过阅读 NoSQL data modeling、观看 Firebase for SQL developers 并尝试各种方法,然后再尝试任何特定的方法,来学习更多关于 NoSQL 数据建模的知识。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2013-02-21
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多