कई ऐप्लिकेशन, पहले पेज के लोड होने पर सभी उपयोगकर्ताओं को एक ही कॉन्टेंट दिखाते हैं. उदाहरण के लिए, कोई समाचार साइट ताज़ा खबरें दिखा सकती है या कोई ई-कॉमर्स साइट सबसे ज़्यादा बिकने वाले आइटम दिखा सकती है.
अगर यह कॉन्टेंट Cloud Firestore से दिखाया जाता है, तो ऐप्लिकेशन लोड होने पर हर उपयोगकर्ता उसी नतीजे के लिए एक नई क्वेरी जारी करेगा. इन नतीजों को उपयोगकर्ताओं के बीच कैश मेमोरी में सेव नहीं किया जाता है. इसलिए, ऐप्लिकेशन धीमा होता है और ज़रूरत से ज़्यादा महंगा होता है.
समाधान: बंडल
Cloud Firestore बंडल, आपको Firebase एडमिन SDK टूल का इस्तेमाल करके बैकएंड पर, क्वेरी के सामान्य नतीजों से डेटा बंडल जोड़ने की अनुमति देते हैं. साथ ही, सीडीएन पर कैश मेमोरी में सेव किए गए, पहले से कंप्यूट किए गए इन ब्लॉब को दिखाते हैं. इससे आपके उपयोगकर्ताओं को पहली बार लोड होने का तेज़ अनुभव मिलता है और आपकी Cloud Firestore क्वेरी की लागत कम हो जाती है.
इस गाइड में हम बंडल और Firebase होस्टिंग के लिए Cloud Functions का इस्तेमाल करेंगे, ताकि बंडल कॉन्टेंट को डाइनैमिक तरीके से कैश मेमोरी में सेव और दिखाया जा सके. बंडल के बारे में ज़्यादा जानकारी गाइड में उपलब्ध है.
सबसे पहले, 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 होस्टिंग सीडीएन, क्लाउड फ़ंक्शन की सेट की गई कैश सेटिंग के मुताबिक बंडल का कॉन्टेंट दिखाएगा. कैश मेमोरी की समयसीमा खत्म होने पर, यह फ़ंक्शन को फिर से ट्रिगर करके कॉन्टेंट को रीफ़्रेश करेगा.
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,00,000 दस्तावेज़ पढ़ने के बजाय, 12 x 24 x 50 = 14,400 दस्तावेज़ पढ़े जाएंगे. Firebase होस्टिंग और Cloud Functions के लिए लगने वाले छोटे-छोटे शुल्क, Cloud Firestore की लागत में होने वाली बचत से आसानी से चुकाए जा सकते हैं.
हालांकि, इससे डेवलपर को ज़्यादा फ़ायदा मिलता है, लेकिन उपयोगकर्ता को इसका सबसे ज़्यादा फ़ायदा होता है. ये 50 दस्तावेज़ सीधे Cloud Firestore के बजाय, Firebase होस्टिंग सीडीएन से लोड करने पर पेज के कॉन्टेंट लोड होने में लगने वाले समय से, आसानी से 100-200 मि॰से॰ या उससे ज़्यादा शेव किए जा सकते हैं. कई अध्ययनों से बार-बार यह पता चला है कि तेज़ी से लोड होने वाले पेजों का मतलब है कि उपयोगकर्ता खुश रहते हैं.