Challenge - 5 Problems
Node-Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this caching example?
Consider this Express route using node-cache. What will be the response when the route is called twice in a row?
Express
import express from 'express'; import NodeCache from 'node-cache'; const app = express(); const cache = new NodeCache({ stdTTL: 5 }); app.get('/data', (req, res) => { const cachedData = cache.get('key'); if (cachedData) { return res.send(`Cached: ${cachedData}`); } const data = 'fresh-data'; cache.set('key', data); res.send(`New: ${data}`); });
Attempts:
2 left
💡 Hint
Think about what happens when the cache is empty and then filled.
✗ Incorrect
On the first call, the cache is empty, so the route sets and returns fresh data. On the second call, the cache has the data, so it returns the cached value.
❓ state_output
intermediate2:00remaining
What is the cache size after these operations?
Given this code snippet using node-cache, how many keys remain in the cache after execution?
Express
import NodeCache from 'node-cache'; const cache = new NodeCache({ stdTTL: 2 }); cache.set('a', 1); cache.set('b', 2); setTimeout(() => { cache.set('c', 3); }, 1500); setTimeout(() => { console.log(cache.keys().length); }, 3000);
Attempts:
2 left
💡 Hint
Remember the TTL and when keys expire.
✗ Incorrect
Keys 'a' and 'b' expire after 2 seconds. 'c' is set after 1.5 seconds and still valid at 3 seconds, so only 'c' remains.
🔧 Debug
advanced2:00remaining
Why does this cache retrieval always return undefined?
Look at this Express route using node-cache. Why does it always respond with 'Cache miss' even after setting the value?
Express
import express from 'express'; import NodeCache from 'node-cache'; const app = express(); const cache = new NodeCache(); app.get('/item', (req, res) => { const value = cache.get('item'); if (value) { return res.send(`Cached: ${value}`); } cache.set('item', 'value'); res.send('Cache miss'); });
Attempts:
2 left
💡 Hint
Check if the cache instance is reused and if set/get are synchronous.
✗ Incorrect
The cache instance is created once and reused. cache.set is synchronous. The first call finds no key and returns 'Cache miss'. Later calls find the cached value and return it.
📝 Syntax
advanced2:00remaining
Which option correctly sets a cache with a custom TTL?
You want to store a key 'user' with value 'Alice' in node-cache with a TTL of 10 seconds. Which code is correct?
Attempts:
2 left
💡 Hint
Check the node-cache set method signature for TTL parameter.
✗ Incorrect
The set method accepts key, value, and optional ttl as a number in seconds. Option C uses the correct syntax.
🧠 Conceptual
expert2:00remaining
What happens if you set a key with TTL 0 in node-cache?
In node-cache, what is the behavior when you set a key with TTL value 0?
Attempts:
2 left
💡 Hint
Think about what TTL 0 means in caching terms.
✗ Incorrect
TTL 0 means no expiration, so the key stays in cache until manually deleted or cache is cleared.