सीडीएन से बंडल किया गया Firestore कॉन्टेंट दिखाएं

कई ऐप्लिकेशन, पहली बार पेज लोड होने पर सभी उपयोगकर्ताओं को एक जैसा कॉन्टेंट दिखाते हैं. उदाहरण के लिए, कोई समाचार साइट आपको ताज़ा खबरें दिखा सकती है. इसके अलावा, कोई ई-कॉमर्स साइट आपको सबसे ज़्यादा बिकने वाले आइटम दिखा सकती है.

अगर यह कॉन्टेंट Cloud Firestore से दिखाया जाता है, तो हर उपयोगकर्ता को ऐप्लिकेशन लोड करते समय, एक ही नतीजे के लिए नई क्वेरी जारी करनी होगी. इन नतीजों को उपयोगकर्ताओं के बीच कैश नहीं किया जाता. इसलिए, ऐप्लिकेशन को काम करने में ज़्यादा समय लगता है और यह ज़्यादा महंगा होता है.

समाधान: बंडल

Cloud Firestore बंडल की मदद से, Firebase Admin SDK का इस्तेमाल करके बैकएंड पर सामान्य क्वेरी के नतीजों से डेटा बंडल बनाए जा सकते हैं. साथ ही, सीडीएन पर कैश मेमोरी में सेव किए गए इन पहले से कैलकुलेट किए गए ब्लॉब को दिखाया जा सकता है. इससे आपके उपयोगकर्ताओं को पहली बार लोड होने में बहुत कम समय लगता है और आपकी Cloud Firestore क्वेरी की लागत कम हो जाती है.

इस गाइड में, हम बंडल जनरेट करने के लिए Cloud Functions का इस्तेमाल करेंगे. साथ ही, बंडल के कॉन्टेंट को डाइनैमिक तौर पर कैश मेमोरी में सेव करने और उसे दिखाने के लिए Firebase Hosting का इस्तेमाल करेंगे. बंडलों के बारे में ज़्यादा जानकारी गाइड में उपलब्ध है.

सबसे पहले, एक सामान्य सार्वजनिक एचटीटीपी फ़ंक्शन बनाएं. इससे 50 नई "स्टोरी" के बारे में क्वेरी की जा सकेगी और नतीजे को बंडल के तौर पर दिखाया जा सकेगा.

Node.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);
});
      
Java

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()));
  }
}
      

इसके बाद, इस Cloud Function को दिखाने और कैश मेमोरी में सेव करने के लिए, Firebase Hosting को कॉन्फ़िगर करें. इसके लिए, firebase.json में बदलाव करें. इस कॉन्फ़िगरेशन के साथ, Firebase Hosting CDN, बंडल किए गए कॉन्टेंट को Cloud Function की सेट की गई कैश मेमोरी की सेटिंग के हिसाब से उपलब्ध कराएगा. कैश मेमोरी की समयसीमा खत्म होने पर, यह फ़ंक्शन को फिर से ट्रिगर करके कॉन्टेंट को रीफ़्रेश करेगा.

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

आखिर में, अपने वेब ऐप्लिकेशन में सीडीएन से बंडल किया गया कॉन्टेंट फ़ेच करें और उसे 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
  // ...
}

अनुमानित बचत

मान लें कि किसी समाचार वेबसाइट पर हर दिन 1,00,000 उपयोगकर्ता आते हैं. साथ ही, हर उपयोगकर्ता शुरुआती लोड पर एक जैसी 50 खास खबरें लोड करता है. कैश मेमोरी में सेव किए बिना, Cloud Firestore से हर दिन 50 x 1,00,000 = 50,00,000 दस्तावेज़ पढ़े जाएंगे.

अब मान लें कि साइट ने ऊपर दी गई तकनीक का इस्तेमाल किया है और उन 50 नतीजों को पांच मिनट तक के लिए कैश मेमोरी में सेव किया है. इसलिए, हर उपयोगकर्ता के लिए क्वेरी के नतीजे लोड करने के बजाय, नतीजे हर घंटे में सिर्फ़ 12 बार लोड किए जाते हैं. साइट पर कितने भी उपयोगकर्ता आएं, Cloud Firestore को की जाने वाली क्वेरी की संख्या में कोई बदलाव नहीं होता. इस पेज पर हर दिन 50 लाख दस्तावेज़ों को पढ़ने के बजाय, 12 x 24 x 50 = 14,400 दस्तावेज़ों को पढ़ा जाएगा. Firebase Hosting और Cloud Functions के लिए, कुछ अतिरिक्त शुल्क लगता है. हालांकि, Cloud Firestore की वजह से होने वाली बचत से यह शुल्क आसानी से पूरा हो जाता है.

डेवलपर को लागत में बचत का फ़ायदा मिलता है. हालांकि, इसका सबसे ज़्यादा फ़ायदा उपयोगकर्ता को मिलता है. इन 50 दस्तावेज़ों को सीधे Cloud Firestore से लोड करने के बजाय, Firebase Hosting CDN से लोड करने पर, पेज के कॉन्टेंट को लोड होने में लगने वाले समय में 100 से 200 मि॰से॰ या इससे ज़्यादा की बचत हो सकती है. अध्ययनों से बार-बार यह पता चला है कि तेज़ी से लोड होने वाले पेजों को लोग ज़्यादा पसंद करते हैं.