कई ऐप्लिकेशन, पहली बार पेज लोड होने पर सभी उपयोगकर्ताओं को एक जैसा कॉन्टेंट दिखाते हैं. उदाहरण के लिए, कोई समाचार साइट ताज़ा खबरें दिखा सकती है या कोई ई-कॉमर्स साइट सबसे ज़्यादा बिकने वाले आइटम दिखा सकती है.
अगर यह कॉन्टेंट 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())); } }
इसके बाद, firebase.json में बदलाव करके, इस Cloud फ़ंक्शन को दिखाने और कैश मेमोरी में सेव करने के लिए, Firebase होस्टिंग को कॉन्फ़िगर करें. इस कॉन्फ़िगरेशन की मदद से, Firebase Hosting सीडीएन, Cloud फ़ंक्शन की सेट की गई कैश मेमोरी की सेटिंग के मुताबिक बंडल का कॉन्टेंट दिखाएगा. कैश मेमोरी की समयसीमा खत्म होने पर, फ़ंक्शन को फिर से ट्रिगर करके कॉन्टेंट को रीफ़्रेश किया जाएगा.
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
// ...
}
अनुमानित बचत
मान लें कि किसी समाचार वेबसाइट पर हर दिन 100,000 उपयोगकर्ता आते हैं और हर उपयोगकर्ता, शुरुआती लोड पर एक जैसी 50 मुख्य खबरें लोड करता है. कैश मेमोरी में सेव न होने पर, Cloud Firestore से हर दिन 50 x 1,00,000 = 50,00,000 दस्तावेज़ पढ़े जाएंगे.
अब मान लें कि साइट ऊपर बताई गई तकनीक को अपनाती है और उन 50 नतीजों को ज़्यादा से ज़्यादा पांच मिनट के लिए कैश मेमोरी में सेव करती है. इसलिए, हर उपयोगकर्ता के लिए क्वेरी के नतीजे लोड करने के बजाय, नतीजे हर घंटे में ठीक 12 बार लोड किए जाते हैं. साइट पर कितने भी उपयोगकर्ता आएं, Cloud Firestore पर क्वेरी की संख्या उतनी ही रहती है. इस पेज पर, 50,00,000 दस्तावेज़ों को पढ़ने के बजाय, हर दिन 12 x 24 x 50 = 14,400 दस्तावेज़ों को पढ़ा जाएगा. Firebase होस्टिंग और Cloud Functions के लिए लगने वाले छोटे-मोटे अतिरिक्त शुल्क, Cloud Firestore की बचत से आसानी से ऑफ़सेट हो जाते हैं.
लागत में बचत होने से डेवलपर को फ़ायदा होता है. हालांकि, इसका सबसे ज़्यादा फ़ायदा उपयोगकर्ता को मिलता है. इन 50 दस्तावेज़ों को सीधे Cloud Firestore से लोड करने के बजाय, Firebase होस्टिंग सीडीएन से लोड करने पर, पेज के कॉन्टेंट को लोड होने में लगने वाले समय को 100 से 200 मिलीसेकंड या उससे ज़्यादा कम किया जा सकता है. स्टडी से बार-बार पता चला है कि तेज़ी से लोड होने वाले पेजों से उपयोगकर्ता ज़्यादा खुश होते हैं.