Web Performance Optimization & Lighthouse · درس

تخزين استجابات API مؤقتًا وضغطها

سرّع استجابات الواجهة الخلفية باستخدام طبقات تخزين مؤقت داخل الذاكرة وموزعة، وإبطال ذكي للتخزين المؤقت، وتقليل حجم البيانات، كي تنجز الخوادم عملًا أقل لكل طلب.

الدرس 4 من 413 خطوة

تخزين استجابات API مؤقتًا وضغطها درس مجاني في Web Performance Optimization & Lighthouse على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web Performance Optimization & Lighthouse، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web Performance Optimization & Lighthouse 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Cache on the Backend?

Recomputing the same response for every request wastes CPU and database time. Caching stores computed results so repeat requests return instantly, cutting both latency and load.

Layers of Caching

  • In-process memory fastest, but per-instance.
  • Distributed cache (Redis/Memcached) shared across servers.
  • HTTP/CDN cache at the edge.

A Simple Cache-Aside Pattern

The most common pattern: check the cache, return on hit, otherwise compute, store, and return. This is called cache-aside.

async function getUser(id) {
  const hit = await redis.get('user:' + id);
  if (hit) return JSON.parse(hit);
  const user = await db.findUser(id);
  await redis.set('user:' + id, JSON.stringify(user), 'EX', 300);
  return user;
}

Choosing a TTL

A time to live balances freshness against hit rate. Volatile data needs short TTLs; reference data can live much longer. Always set some expiry to avoid stale buildup.

Invalidation Strategies

The hard part of caching is invalidation. On writes, either delete the affected keys or update them (write-through). Stale data here is a common production bug.

async function updateUser(id, data) {
  await db.update(id, data);
  await redis.del('user:' + id);
}

HTTP Caching Headers

For cacheable API responses, set Cache-Control so browsers and CDNs can reuse them, removing the request entirely on a hit.

res.set('Cache-Control', 'public, max-age=60, stale-while-revalidate=300');

Conditional Requests

ETag and If-None-Match let the server reply 304 Not Modified with no body when data is unchanged, saving bandwidth.

res.set('ETag', hashOf(payload));
// next time: if If-None-Match matches, send 304

Shrinking the Payload

Return only the fields clients need, paginate large lists, and avoid over-fetching. Smaller payloads serialize faster and transfer quicker.

Compressing Responses

Enable gzip or Brotli on JSON responses. Combined with caching, this minimizes both compute and transfer per request.

const compression = require('compression');
app.use(compression());

Avoiding Stampedes

When a hot key expires, many requests may hit the database at once (a cache stampede). Mitigate with locks, request coalescing, or stale-while-revalidate.

Strategy Summary

  • Cache-aside with sensible TTLs.
  • Invalidate on writes.
  • Use Cache-Control and ETags.
  • Trim and compress payloads.
  • Guard against stampedes.

Quick Check

After a user updates their profile, the API keeps returning the old data for several minutes. What is the most likely cause?

Recap

You learned to cut backend work with layered caching (cache-aside, TTLs, invalidation on writes), HTTP caching via Cache-Control and ETags, payload trimming, and compression, while guarding against cache stampedes.

البدء مجانًا

تعلم Web Performance Optimization & Lighthouse مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تخزين استجابات API مؤقتًا وضغطها» مجاني؟

نعم — نص درس «تخزين استجابات API مؤقتًا وضغطها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web Performance Optimization & Lighthouse، انتقل إلى CoddyKit PRO. تتضمن دورة Web Performance Optimization & Lighthouse 4 دروس في المجموع.

ماذا ستتعلم في «تخزين استجابات API مؤقتًا وضغطها»؟

سرّع استجابات الواجهة الخلفية باستخدام طبقات تخزين مؤقت داخل الذاكرة وموزعة، وإبطال ذكي للتخزين المؤقت، وتقليل حجم البيانات، كي تنجز الخوادم عملًا أقل لكل طلب. تتمرن على Web Performance Optimization & Lighthouse مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Web Performance Optimization & Lighthouse؟

لا تُشترط خبرة سابقة. Web Performance Optimization & Lighthouse على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تخزين استجابات API مؤقتًا وضغطها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Web Performance Optimization & Lighthouse هذا؟

نعم. كل درس في Web Performance Optimization & Lighthouse يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. اختناقات أداء الواجهة الخلفية
  2. تحسين استعلامات قاعدة البيانات
  3. تأثير العرض من جهة الخادم (SSR)
  4. تخزين استجابات API مؤقتًا وضغطها
← العودة إلى Web Performance Optimization & Lighthouse