استراتيجيات التخزين المؤقت لـ Node.js
طبّقوا آليات متنوعة للتخزين المؤقت، مثل Redis، لتحسين أوقات الاستجابة وتقليل الحمل على قاعدة البيانات
استراتيجيات التخزين المؤقت لـ Node.js درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Node.js Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What is Caching?
Welcome to Caching Strategies for Node.js! Caching is a fundamental technique to boost application performance and reduce load on your backend services.
- Imagine your app frequently asks for the same data. Instead of fetching it repeatedly, caching stores a copy closer to the user.
- This stored data, or 'cache', can be retrieved much faster than going to the original source, like a database.
- It's like having a quick-access shortcut for frequently needed information!

Why Cache in Node.js?
Node.js applications often serve many requests, and database operations can be slow. Caching helps significantly:
- Faster Response Times: Users get data quicker, improving their experience.
- Reduced Database Load: Fewer requests hit your database, saving resources and preventing bottlenecks.
- Improved Scalability: Your application can handle more users without needing to scale up your database as aggressively.
It's a key strategy for high-performance Node.js services.
How Caching Works
The caching process follows a simple flow:
- A request comes in for specific data.
- The application first checks if the data is available in the cache. This is called a 'cache hit'.
- If it's a cache hit, the data is returned immediately from the cache.
- If it's a cache miss (data not found), the application fetches the data from the original source (e.g., a database).
- After fetching, the data is stored in the cache for future requests, then returned to the user.
In-Memory Caching
One of the simplest caching methods is in-memory caching. This means storing data directly within your Node.js application's process memory.
- It's very fast because there's no network overhead.
- Easy to implement using basic JavaScript objects or
Maps. - However, cached data is lost if the server restarts.
- It's not shared across multiple instances of your Node.js application (e.g., if you're running multiple processes or servers).
Best for small, localized caches.
In-Memory Cache Example
Let's see a simple in-memory cache using a JavaScript Map. We'll simulate a slow database call.
const cache = new Map();
// Simulate a slow database call
function getProductFromDB(id) {
console.log(`Fetching product ${id} from DB...`);
return new Promise(resolve => {
setTimeout(() => {
resolve({ id: id, name: `Product ${id}`, price: 10 + id });
}, 1000); // Simulate 1-second delay
});
}
async function getProduct(id) {
if (cache.has(id)) {
console.log(`Cache hit for product ${id}`);
return cache.get(id);
}
const product = await getProductFromDB(id);
cache.set(id, product);
console.log(`Cache miss, stored product ${id}`);
return product;
}
// Test the caching
(async () => {
console.log("First call (miss):");
await getProduct(1);
console.log("\nSecond call (hit):");
await getProduct(1);
console.log("\nThird call (new product):");
await getProduct(2);
})();Introducing Redis for Caching
For more robust and scalable caching, especially in distributed systems, a dedicated cache store like Redis is ideal.
- Redis is an in-memory data structure store, used as a database, cache, and message broker.
- It's incredibly fast and supports various data types (strings, hashes, lists, sets, etc.).
- Crucially, Redis can be run as a separate service, allowing multiple Node.js instances to share the same cache.
- It also offers persistence options, so data isn't lost on restart.
Connecting Node.js to Redis
To use Redis in Node.js, we need a client library. ioredis is a popular choice for its performance and features.
First, install it: npm install ioredis
Then, connect to your Redis server and perform basic operations like SET (store data) and GET (retrieve data).
const Redis = require('ioredis');
const redis = new Redis(); // Connects to localhost:6379 by default
(async () => {
try {
console.log("Connected to Redis!");
// Store a value
await redis.set('myKey', 'Hello from Redis!');
console.log("Set 'myKey'");
// Retrieve a value
const value = await redis.get('myKey');
console.log(`Retrieved 'myKey': ${value}`);
// Try to get a non-existent key
const nonExistent = await redis.get('nonExistentKey');
console.log(`Retrieved 'nonExistentKey': ${nonExistent}`); // Will be null
} catch (err) {
console.error("Redis error:", err);
} finally {
redis.quit(); // Disconnect from Redis
}
})();Caching API Responses with Redis
Let's integrate Redis into a simple Express.js API route to cache responses. This pattern is very common for frequently accessed data.
const express = require('express');
const Redis = require('ioredis');
const app = express();
const port = 3000;
const redis = new Redis();
// Simulate a slow database call
const getExpensiveData = async (id) => {
console.log(`Fetching data for ${id} from 'database'...`);
return new Promise(resolve => {
setTimeout(() => {
resolve({ id: id, info: `Expensive data for ${id}` });
}, 1500); // Simulate 1.5-second delay
});
};
app.get('/data/:id', async (req, res) => {
const dataId = req.params.id;
const cacheKey = `data:${dataId}`;
try {
// 1. Check cache first
const cachedData = await redis.get(cacheKey);
if (cachedData) {
console.log(`Cache hit for ${cacheKey}`);
return res.json(JSON.parse(cachedData));
}
// 2. Cache miss, fetch from source
console.log(`Cache miss for ${cacheKey}`);
const data = await getExpensiveData(dataId);
// 3. Store in cache (with expiration) and return
await redis.setex(cacheKey, 60, JSON.stringify(data)); // Cache for 60 seconds
res.json(data);
} catch (error) {
console.error('API error:', error);
res.status(500).send('Server error');
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log("Try visiting http://localhost:3000/data/1 multiple times.");
});Cache Invalidation Strategies
A critical aspect of caching is managing stale data. If the original data changes, your cache might still hold the old version. This is where invalidation comes in.
- Time-to-Live (TTL): Automatically expire cached items after a set duration. Simplest and most common.
- Manual Invalidation: Explicitly remove an item from the cache when its source data changes (e.g., after a database update).
- Write-Through/Write-Back: Update the cache simultaneously when writing to the database (write-through) or after the database write is confirmed (write-back).
Implementing TTL with Redis
Redis makes implementing TTL easy with commands like EXPIRE or SETEX. SETEX is particularly useful as it sets a key and its expiration in one atomic operation.
const Redis = require('ioredis');
const redis = new Redis();
(async () => {
try {
const key = 'temporary_message';
const value = 'This message expires in 10 seconds.';
const ttl = 10; // seconds
console.log(`Setting '${key}' with TTL of ${ttl} seconds.`);
await redis.setex(key, ttl, value);
let retrievedValue = await redis.get(key);
console.log(`Immediately after set: ${retrievedValue}`);
console.log(`Waiting for ${ttl + 1} seconds...`);
await new Promise(resolve => setTimeout(resolve, (ttl + 1) * 1000));
retrievedValue = await redis.get(key);
console.log(`After expiration: ${retrievedValue}`); // Should be null
} catch (err) {
console.error("Redis error:", err);
} finally {
redis.quit();
}
})();Caching Strategies Quiz
Which of the following are benefits of implementing caching in a Node.js application?
Recap: Caching Strategies
Congratulations! You've explored essential caching strategies for Node.js.
- We learned that caching boosts performance by storing frequently accessed data, reducing database hits and improving response times.
- We explored simple in-memory caching for local, fast access.
- For distributed and scalable solutions, Redis stands out, offering robust features and shared caching across multiple instances.
- Finally, we covered critical cache invalidation strategies like TTL to manage stale data.
Mastering caching is crucial for building high-performance Node.js applications.
الأسئلة الشائعة
هل درس «استراتيجيات التخزين المؤقت لـ Node.js» مجاني؟
نعم — نص درس «استراتيجيات التخزين المؤقت لـ Node.js» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «استراتيجيات التخزين المؤقت لـ Node.js»؟
طبّقوا آليات متنوعة للتخزين المؤقت، مثل Redis، لتحسين أوقات الاستجابة وتقليل الحمل على قاعدة البيانات تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Node.js Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. Node.js Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «استراتيجيات التخزين المؤقت لـ Node.js»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Node.js Backend Development Bootcamp هذا؟
نعم. كل درس في Node.js Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- استراتيجيات التخزين المؤقت لـ Node.js
- موازنة التحميل لتطبيقات Node.js
- تحسين حلقة الأحداث في Node.js
- تحليل الأداء واكتشاف تسرّب الذاكرة