แสดงเนื้อหา Firestore แบบรวมจาก CDN

แอปพลิเคชันจำนวนมากให้บริการเนื้อหาเดียวกันแก่ผู้ใช้ทุกคนเมื่อโหลดหน้าแรก ตัวอย่างเช่น เว็บไซต์ข่าวอาจแสดงเรื่องราวล่าสุด หรือไซต์อีคอมเมิร์ซอาจแสดงรายการที่ขายดีที่สุด

หากเนื้อหานี้ให้บริการจาก Cloud Firestore ผู้ใช้แต่ละรายจะออกแบบสอบถามใหม่สำหรับผลลัพธ์เดียวกันเมื่อโหลดแอปพลิเคชัน เนื่องจากผลลัพธ์เหล่านี้ไม่ได้ถูกแคชไว้ระหว่างผู้ใช้ แอปพลิเคชันจึงช้ากว่าและมีราคาแพงกว่าที่จำเป็น

วิธีแก้ปัญหา: การรวมกลุ่ม

ชุด Cloud Firestore ช่วยให้คุณสามารถรวบรวมชุดข้อมูลจากผลลัพธ์การสืบค้นทั่วไปบนแบ็กเอนด์โดยใช้ Firebase Admin SDK และให้บริการ Blob ที่คำนวณล่วงหน้าเหล่านี้ซึ่งแคชไว้บน CDN ซึ่งจะทำให้ผู้ใช้ได้รับประสบการณ์การโหลดครั้งแรกที่รวดเร็วยิ่งขึ้น และลดต้นทุนการค้นหา Cloud Firestore ของคุณ

ในคู่มือนี้ เราจะใช้ฟังก์ชันคลาวด์เพื่อสร้างบันเดิลและโฮสติ้ง Firebase เพื่อแคชและให้บริการเนื้อหาบันเดิลแบบไดนามิก ข้อมูลเพิ่มเติมเกี่ยวกับชุดรวมมีอยู่ใน คำแนะนำ

ขั้นแรกให้สร้างฟังก์ชัน HTTP สาธารณะแบบง่ายๆ เพื่อสืบค้น "เรื่องราว" ล่าสุด 50 รายการ และให้บริการผลลัพธ์เป็นกลุ่ม

โหนด js
exports.createBundle = functions.https.onRequest(async (request, response) => {
  // Query the 50 latest stories
  const latestStories = await db.collection('stories')
    .orderBy('timestamp', 'desc')
    .limit(50)
    .get();

  // Build the bundle from the query results
  const bundleBuffer = db.bundle('latest-stories')
    .add('latest-stories-query', latestStories)
    .build();

  // Cache the response for up to 5 minutes;
  // see https://firebase.google.com/docs/hosting/manage-cache
  response.set('Cache-Control', 'public, max-age=300, s-maxage=600');

  response.end(bundleBuffer);
});
      
ชวา

package com.example;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreBundle;
import com.google.cloud.firestore.Query.Direction;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.cloud.FirestoreClient;
import java.io.BufferedWriter;
import java.io.IOException;

public class ExampleFunction implements HttpFunction {

  public static FirebaseApp initializeFirebase() throws IOException {
    if (FirebaseApp.getApps().isEmpty()) {
      FirebaseOptions options = FirebaseOptions.builder()
          .setCredentials(GoogleCredentials.getApplicationDefault())
          .setProjectId("YOUR-PROJECT-ID")
          .build();

      FirebaseApp.initializeApp(options);
    }

    return FirebaseApp.getInstance();
  }

  @Override
  public void service(HttpRequest request, HttpResponse response) throws Exception {
    // Get a Firestore instance
    FirebaseApp app = initializeFirebase();
    Firestore db = FirestoreClient.getFirestore(app);

    // Query the 50 latest stories
    QuerySnapshot latestStories = db.collection("stories")
        .orderBy("timestamp", Direction.DESCENDING)
        .limit(50)
        .get()
        .get();

    // Build the bundle from the query results
    FirestoreBundle bundle = db.bundleBuilder("latest-stores")
        .add("latest-stories-query", latestStories)
        .build();

    // Cache the response for up to 5 minutes
    // see https://firebase.google.com/docs/hosting/manage-cache
    response.appendHeader("Cache-Control", "public, max-age=300, s-maxage=600");

    // Write the bundle to the HTTP response
    BufferedWriter writer = response.getWriter();
    writer.write(new String(bundle.toByteBuffer().array()));
  }
}
      

ถัดไปกำหนดค่า Firebase Hosting เพื่อให้บริการและแคชฟังก์ชันคลาวด์นี้โดยการแก้ไข firebase.json ด้วยการกำหนดค่านี้ Firebase Hosting CDN จะให้บริการเนื้อหาบันเดิลตามการตั้งค่าแคชที่กำหนดโดยฟังก์ชันคลาวด์ เมื่อแคชหมดอายุ แคชจะรีเฟรชเนื้อหาโดยเรียกใช้ฟังก์ชันอีกครั้ง

firebase.json
{
  "hosting": {
    // ...
    "rewrites": [{
      "source": "/createBundle",
      "function": "createBundle"
    }]
  },
  // ...
}

สุดท้ายในเว็บแอปพลิเคชันของคุณ ให้ดึงเนื้อหาที่รวมกลุ่มจาก CDN และโหลดลงใน Firestore SDK

// If you are using module bundlers.
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/firestore/bundle" // This line enables bundle loading as a side effect.

async function fetchFromBundle() {
  // 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('/createBundle');

  // Load the bundle contents into the Firestore SDK
  await db.loadBundle(resp.body);

  // Query the results from the cache
  // Note: omitting "source: cache" will query the Firestore backend.
  
  const query = await db.namedQuery('latest-stories-query');
  const storiesSnap = await query.get({ source: 'cache' });

  // Use the results
  // ...
}

ประหยัดโดยประมาณ

พิจารณาเว็บไซต์ข่าวที่มีผู้ใช้ 100,000 รายต่อวัน และผู้ใช้แต่ละรายโหลดเรื่องเด่น 50 เรื่องเดียวกันในการโหลดครั้งแรก หากไม่มีแคชใดๆ จะส่งผลให้มีการอ่านเอกสาร 50 x 100,000 = 5,000,000 ครั้งต่อวันจาก Cloud Firestore

ตอนนี้ สมมติว่าไซต์ใช้เทคนิคข้างต้นและแคชผลลัพธ์ 50 รายการเหล่านั้นเป็นเวลาสูงสุด 5 นาที ดังนั้นแทนที่จะโหลดผลลัพธ์การสืบค้นสำหรับผู้ใช้ทุกคน ผลลัพธ์จะถูกโหลด 12 ครั้งต่อชั่วโมงพอดี ไม่ว่าผู้ใช้จะมาถึงไซต์จำนวนเท่าใด จำนวนการสืบค้นไปยัง Cloud Firestore ก็ยังคงเท่าเดิม แทนที่จะอ่านเอกสาร 5,000,000 ครั้ง หน้านี้จะใช้การอ่านเอกสาร 12 x 24 x 50 = 14,400 ครั้งต่อวัน ค่าใช้จ่ายเพิ่มเติมเล็กน้อยสำหรับโฮสติ้ง Firebase และฟังก์ชันคลาวด์สามารถชดเชยได้อย่างง่ายดายด้วยการประหยัดต้นทุนของ Cloud Firestore

แม้ว่านักพัฒนาจะได้รับประโยชน์จากการประหยัดต้นทุน แต่ผู้ที่ได้ประโยชน์มากที่สุดก็คือผู้ใช้ การโหลดเอกสาร 50 รายการนี้จาก Firebase Hosting CDN แทนที่จะโหลดจาก Cloud Firestore โดยตรงสามารถลดเวลาในการโหลดเนื้อหาของหน้าเว็บได้ 100-200 มิลลิวินาทีขึ้นไป ผลการศึกษาแสดงให้เห็นซ้ำแล้วซ้ำอีกว่าหน้าเว็บที่รวดเร็วหมายถึงผู้ใช้ที่มีความสุขมากขึ้น