0
0
Expressframework~20 mins

Cache invalidation strategies in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Cache Invalidation Types
Which cache invalidation strategy immediately removes cached data when the underlying data changes?
ATime-based expiration where cache clears after a set time
BLazy invalidation where cache is cleared only on next access
CManual invalidation triggered by data update events
DCache warming that preloads data before expiration
Attempts:
2 left
💡 Hint
Think about which method reacts instantly to data changes.
component_behavior
intermediate
2: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();
  }
});
AThe cache never expires because setTimeout is not cleared
BThe second request triggers next() and fetches fresh data
CThe cache clears immediately after the first request
DThe second request returns cached data instantly without calling next()
Attempts:
2 left
💡 Hint
Consider what happens when cache[key] exists.
🔧 Debug
advanced
2: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');
});
ACache key uses req.originalUrl which differs from cached GET request URL
Bdelete operator cannot remove properties from cache object
CupdateData is asynchronous but cache deletion runs before update completes
Dres.send should be called before deleting cache
Attempts:
2 left
💡 Hint
Check if the cache key matches the cached data key.
📝 Syntax
advanced
2: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
}
Acache.remove(key);
Bdelete cache[key];
Ccache[key] = null;
Dcache.pop(key);
Attempts:
2 left
💡 Hint
Think about how to remove a property from an object in JavaScript.
state_output
expert
3: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();
  }
});
A['/data', '/info']
B['/data']
C['/info']
D[]
Attempts:
2 left
💡 Hint
Remember the POST request deletes '/data' cache key.