Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the NodeCache library.
Express
const NodeCache = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'node-cache'.
Mixing up package names like 'memory-cache'.
✗ Incorrect
The correct package name to import is node-cache.
2fill in blank
mediumComplete the code to create a new cache instance.
Express
const cache = new NodeCache({ stdTTL: [1] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting TTL to 0 disables expiration, which is not typical here.
Using a very large number like 1000 seconds without reason.
✗ Incorrect
The stdTTL option sets the default time-to-live in seconds. 60 seconds is a common choice.
3fill in blank
hardFix the error in setting a cache value with key 'user' and value 'John'.
Express
cache.[1]('user', 'John');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'set' causes no data to be saved.
Using 'add' or 'put' which are not methods in node-cache.
✗ Incorrect
The method to store a value in the cache is set.
4fill in blank
hardFill both blanks to retrieve a cached value and handle a cache miss.
Express
const value = cache.[1]('key'); if (value === [2]) { console.log('Cache miss'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for
null instead of undefined causes missed cache misses.Using 'set' instead of 'get' to retrieve data.
✗ Incorrect
Use get to retrieve the value. If the key is missing, get returns undefined.
5fill in blank
hardFill all three blanks to create a cache, set a value, and retrieve it.
Express
const cache = new NodeCache({ stdTTL: [1] });
cache.[2]('color', 'blue');
const color = cache.[3]('color'); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete' instead of 'get' to retrieve data.
Setting TTL to 0 disables expiration unintentionally.
✗ Incorrect
We create a cache with TTL 120 seconds, set the key 'color' to 'blue', then get it back.