0
0
Expressframework~10 mins

Cache invalidation strategies in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a cache expiration time in Express middleware.

Express
app.use((req, res, next) => {
  res.set('Cache-Control', 'public, max-age=[1]');
  next();
});
Drag options to blanks, or click blank then click option'
Aexpires
Bcache
C3600
Dno-cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cache' or 'expires' instead of a numeric value.
Setting 'no-cache' which disables caching instead of setting expiration.
2fill in blank
medium

Complete the code to clear a specific cache key in an Express route.

Express
app.get('/clear-cache', (req, res) => {
  cache.[1]('user-data');
  res.send('Cache cleared');
});
Drag options to blanks, or click blank then click option'
Adelete
Bget
Cset
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' which only retrieves data.
Using 'clear' which removes all cache entries.
3fill in blank
hard

Fix the error in the cache middleware to properly invalidate cache after response.

Express
app.use((req, res, next) => {
  res.on('finish', () => {
    cache.[1](req.originalUrl);
  });
  next();
});
Drag options to blanks, or click blank then click option'
Adelete
Binvalidate
Cset
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' which adds or updates cache instead of removing.
Using 'invalidate' which is not a standard method.
4fill in blank
hard

Fill both blanks to create a cache middleware that skips caching for authenticated users.

Express
app.use((req, res, next) => {
  if (req.user && req.user.[1]) {
    res.set('Cache-Control', '[2]');
  }
  next();
});
Drag options to blanks, or click blank then click option'
AisAuthenticated
Bno-store
Cpublic
DisAdmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isAdmin' instead of 'isAuthenticated'.
Setting 'public' which allows caching instead of disabling it.
5fill in blank
hard

Fill all three blanks to implement a cache invalidation strategy that clears cache on data update.

Express
app.post('/update-data', (req, res) => {
  updateDatabase(req.body);
  cache.[1](req.body.[2]);
  res.status(200).send({ message: '[3]' });
});
Drag options to blanks, or click blank then click option'
Adelete
Bid
CData updated and cache cleared
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'delete' to remove cache.
Using wrong key like 'key' instead of 'id'.
Sending a generic message instead of a clear confirmation.