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

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

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

समाधान: बंडल

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

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

सबसे पहले 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()));
  }
}
      

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

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

आखिर में, अपने वेब ऐप्लिकेशन में, बंडल किए गए कॉन्टेंट को सीडीएन से फ़ेच करें और लोड करें को Firebase 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 मुख्य खबरें दिखती हैं. किसी भी कैश मेमोरी के बिना, ऐसा हो सकता है 50 x 1,00,000 = Cloud Firestore से हर दिन 50,00,000 दस्तावेज़ पढ़ना.

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

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