Challenge - 5 Problems
Cache Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding Cache Invalidation Types
Which cache invalidation strategy immediately removes cached data when the underlying data changes?
Attempts:
2 left
💡 Hint
Think about which method reacts instantly to data changes.
✗ Incorrect
Manual invalidation triggers cache removal right when data changes, ensuring fresh data immediately.
❓ component_behavior
intermediate2:00remaining
Cache Behavior in Express Middleware
Given an Express middleware that caches responses with a TTL of 10 seconds, what happens if a client requests the same resource twice within 5 seconds?
Express
app.use((req, res, next) => {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
cache[key] = body;
setTimeout(() => { delete cache[key]; }, 10000);
res.sendResponse(body);
};
next();
}
});Attempts:
2 left
💡 Hint
Consider what happens when cache[key] exists.
✗ Incorrect
If cache[key] exists, the middleware sends cached data directly and does not call next(), so no fresh data fetch occurs.
🔧 Debug
advanced2:00remaining
Identifying Cache Invalidation Bug
Why does this Express cache invalidation code fail to clear the cache after data update?
Express
app.post('/update', (req, res) => { updateData(req.body); delete cache[req.originalUrl]; res.send('Updated'); });
Attempts:
2 left
💡 Hint
Check if the cache key matches the cached data key.
✗ Incorrect
The POST request URL differs from the GET request URL used as cache key, so deleting cache[req.originalUrl] does not remove the cached GET data.
📝 Syntax
advanced2:00remaining
Correct Cache Invalidation Syntax
Which option correctly invalidates a cache entry for a given key in an Express app using a simple object cache?
Express
const cache = {};
function invalidateCache(key) {
// Invalidate cache here
}Attempts:
2 left
💡 Hint
Think about how to remove a property from an object in JavaScript.
✗ Incorrect
The delete operator removes a property from an object. Other options are invalid or only nullify the value.
❓ state_output
expert3:00remaining
Cache State After Multiple Requests
Consider this Express cache middleware with manual invalidation. After these requests, what keys remain in the cache object?
1. GET /data (cache miss, stores response)
2. POST /data (updates data, invalidates cache)
3. GET /data (cache miss, stores new response)
4. GET /info (cache miss, stores response)
Express
const cache = {};
app.use((req, res, next) => {
if (req.method === 'GET') {
if (cache[req.originalUrl]) {
res.send(cache[req.originalUrl]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
cache[req.originalUrl] = body;
res.sendResponse(body);
};
next();
}
} else if (req.method === 'POST') {
updateData(req.body);
delete cache['/data'];
res.send('Updated');
} else {
next();
}
});Attempts:
2 left
💡 Hint
Remember the POST request deletes '/data' cache key.
✗ Incorrect
After POST /data, cache['/data'] is deleted. Then GET /data stores new response again. Then GET /info stores response. So keys after all requests are '/data' and '/info'.