如何使用 Firestore 数据包和 CDN 在客户端之间共享 Firestore 查询缓存

编程入门 行业动态 更新时间:2024-10-04 23:32:47

如何使用 Firestore 数据包和 CDN 在客户端之间共享 Firestore 查询<a href=https://www.elefans.com/category/jswz/34/1771061.html style=缓存"/>

如何使用 Firestore 数据包和 CDN 在客户端之间共享 Firestore 查询缓存

我正在尝试在用户之间缓存一些 firestore 查询,以通过 Bundles 从 CDN 提供文档。

所以我创建了以下云函数:

exports.getStoreCategoriesBundle = functions.region(DEFAULT_REGION).https.onRequest(async (request: any, response: any) => {

    response.set("Access-Control-Allow-Origin", "*");
    response.set("Access-Control-Allow-Headers", "*");
    response.set("Content-Type", "application/json; charset=utf-8");

    if (!request || !request.query || !request.query.storeId) {
        // end function with 401 header
        response.set("Cache-Control", "private, max-age=0, s-maxage=0").status(401).end("Query data required.");
        return;
    }

    // Query all categories
    const allStoreCategories = await firestore.collection(`stores/${request.query.storeId}/store_categories`)
        .where("status", "==", 1)
        .orderBy("count", "desc")
        .limit(500)
        .get();

    // Build the bundle from the query results
    const bundleBuffer = firestore.bundle("all-store-categories-"+request.query.storeId)
    .add("all-store-categories-query-"+request.query.storeId, allStoreCategories)
    .build();

    // Cache the response for up to 120 minutes (7200 / 60 = 120 minutes);
    response.set("Cache-Control", "public, max-age=7200, s-maxage=14400");

    response.end(bundleBuffer);
});

然后我在客户端加载一个包:

async fetchFromBundle(bundleName: string, queryName: string) {
    // Fetch the bundle from Firebase Hosting, if the CDN cache is hit the 'X-Cache'
    // response header will be set to 'HIT'
    const resp = await fetch(bundleName);

    // Load the bundle contents into the Firestore SDK
    const task = await loadBundle(this.firestore, resp.body);
    console.log("task: " , task);

    // Query the results from the cache
    // Note: omitting "source: cache" will query the Firestore backend.
    const query = await namedQuery(this.firestore, queryName);
    const querySnapshot = await getDocsFromCache(query);

    // Use the results
    
    let docs : DocumentData[] = [];
    querySnapshot.forEach((doc) => {
      docs.push({
        ...doc.data(),
        id: doc.id
        // metadata: doc.metadata,
        // ref: doc.ref,
      });
    });
    return docs;
}

代码成功运行,但我注意到该功能是在云中为每个客户端分别执行的。当函数被执行时,这意味着从 Cloud Firestore 查询文档。

例如:

当我在我的手机设备上调用“fetchFromBundle”时,云功能在谷歌云控制台上执行。

然后,如果我使用上面使用的相同参数再次调用“fetchFromBundle”,但来自不同的设备(如我的桌面),云功能也会在第一个客户端(我的手机设备)生成的缓存期过期之前执行。

我想做的是让 bundle 缓存在来自 Firebase Hosting CDN 的客户端之间共享。因此,第一个客户端必须查询并构建捆绑包,之后的所有其他客户端都必须从 CDN 缓存中加载捆绑包,直到 CDN 缓存过期为止。

提前致谢^^

回答如下:

更多推荐

如何使用 Firestore 数据包和 CDN 在客户端之间共享 Firestore 查询缓存

本文发布于:2024-05-30 21:25:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770940.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   如何使用   数据包   客户端   Firestore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!