CDN থেকে বান্ডেল করা Firestore কন্টেন্ট পরিবেশন করুন

অনেক অ্যাপ্লিকেশন প্রথম পৃষ্ঠা লোড হওয়ার সময় সব ব্যবহারকারীকে একই কন্টেন্ট দেখায়। যেমন, কোনও খবরের সাইট লেটেস্ট খবর দেখাতে পারে অথবা কোনও ই-কমার্স সাইট সবচেয়ে বেশি বিক্রি হওয়া আইটেম দেখাতে পারে।

এই কন্টেন্ট Cloud Firestore থেকে দেখানো হলে, প্রত্যেক ব্যবহারকারী অ্যাপ্লিকেশন লোড করার সময় একই ফলাফলের জন্য নতুন কোয়েরি ইস্যু করবেন। এইসব ফলাফল ব্যবহারকারীদের মধ্যে ক্যাশে করা হয় না বলে, অ্যাপ্লিকেশনটি যতটা হওয়া উচিত তার চেয়ে বেশি ধীরগতির এবং ব্যয়বহুল।

সমাধান: বান্ডেল

Cloud Firestore বান্ডেল আপনাকে Firebase Admin SDK ব্যবহার করে ব্যাকএন্ডে সাধারণ কোয়েরি ফলাফল থেকে ডেটা বান্ডেল একত্রিত করতে এবং CDN-এ ক্যাশে করা এই প্রি-কম্পিউটেড ব্লবগুলি পরিবেশন করতে দেয়। এটি আপনার ব্যবহারকারীদের অনেক দ্রুত প্রথম লোডের অভিজ্ঞতা দেয় এবং আপনার Cloud Firestore কোয়েরির খরচ কমায়।

এই গাইডে আমরা বান্ডেল তৈরি করতে Cloud Functions এবং ডায়নামিকভাবে বান্ডেল কন্টেন্ট ক্যাশে ও পরিবেশন করতে Firebase Hosting ব্যবহার করব। বান্ডেল সম্পর্কে আরও তথ্য গাইড থেকে পাওয়া যাবে।

প্রথমে, ৫০টি লেটেস্ট "স্টোরি" কোয়েরি করতে এবং ফলাফল একটি বান্ডেল হিসেবে পরিবেশন করতে, একটি সাধারণ পাবলিক HTTP ফাংশন তৈরি করুন।

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);
});
      
জাভা

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.json পরিবর্তন করে এই Cloud Function পরিবেশন ও ক্যাশে করার জন্য Firebase Hosting কনফিগার করুন। এই কনফিগারেশনের মাধ্যমে Firebase Hosting CDN Cloud Function-এর মাধ্যমে সেট করা ক্যাশে সেটিংস অনুযায়ী বান্ডেল কন্টেন্ট পরিবেশন করবে। ক্যাশের মেয়াদ শেষ হয়ে গেলে, ফাংশনটি আবার ট্রিগার করে কন্টেন্ট রিফ্রেশ করা হবে।

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
  // ...
}

আনুমানিক সাশ্রয়

ধরা যাক, কোনও খবরের ওয়েবসাইটে প্রতিদিন ১,০০,০০০ জন ব্যবহারকারী আসেন এবং প্রত্যেকেই প্রথমবার সাইট লোড করার সময় একই ৫০টি সেরা খবর লোড করেন। কোনও ক্যাশিং না থাকলে, এর ফলে Cloud Firestore থেকে প্রতিদিন ৫০ x ১০০,০০০ = ৫,০০০,০০০ ডকুমেন্ট রিড হবে।

এখন ধরে নিন যে সাইটটি উপরের কৌশলটি গ্রহণ করে এবং সেই ৫০টি ফলাফল সর্বাধিক ৫ মিনিটের জন্য ক্যাশে করে। তাই প্রত্যেক ব্যবহারকারীর জন্য কোয়েরির ফলাফল লোড করার পরিবর্তে, প্রতি ঘণ্টায় ঠিক ১২ বার ফলাফল লোড করা হয়। সাইটে যতজন ব্যবহারকারীই আসুন না কেন, Cloud Firestore-এ কোয়েরির সংখ্যা একই থাকে। ৫,০০০,০০০ ডকুমেন্ট রিডের পরিবর্তে, এই পৃষ্ঠাটি প্রতিদিন ১২ x ২৪ x ৫০ = ১৪,৪০০ ডকুমেন্ট রিড ব্যবহার করবে। Firebase হোস্টিং এবং Cloud Functions-এর জন্য ছোট অতিরিক্ত খরচ Cloud Firestore খরচ সাশ্রয়ের মাধ্যমে সহজেই অফসেট করা হয়।

খরচ বাঁচানোর ফলে ডেভেলপার লাভবান হলেও, সবচেয়ে বেশি লাভবান হন ব্যবহারকারী। Cloud Firestore থেকে সরাসরি লোড করার পরিবর্তে Firebase হোস্টিং CDN থেকে এই ৫০টি ডকুমেন্ট লোড করলে, পৃষ্ঠার কন্টেন্ট লোড করার সময় থেকে সহজেই ১০০-২০০ মিলি সেকেন্ড বা তার বেশি সময় কমানো যেতে পারে। সমীক্ষায় বারবার দেখা গেছে যে দ্রুত লোড হওয়া পৃষ্ঠা ব্যবহারকারীদের খুশি করে।