What is in-memory caching in the context of a Node.js server?
In-memory caching stores data temporarily inside the server's memory (RAM) to quickly serve repeated requests without recalculating or fetching from a database.
Click to reveal answer
beginner
What is the main purpose of the node-cache library?
node-cache provides a simple way to store and retrieve data in memory with expiration times, helping improve app speed by avoiding repeated heavy operations.
Click to reveal answer
intermediate
How do you set a value in node-cache with a time-to-live (TTL)?
Use cache.set(key, value, ttlInSeconds). The TTL defines how long the data stays in cache before it expires.
Click to reveal answer
intermediate
What happens when you try to get a key from node-cache that has expired or does not exist?
The get method returns undefined, indicating the data is not available in cache and may need to be fetched or recalculated.
Click to reveal answer
beginner
Why is in-memory caching not suitable for storing very large data or permanent storage?
Because memory is limited and data is lost when the server restarts, in-memory caching is best for small, temporary data to speed up responses.
Click to reveal answer
Which of the following best describes node-cache?
AA library to store data temporarily in server memory with expiration.
BA database for permanent data storage.
CA tool to compress files on the server.
DA frontend UI framework.
✗ Incorrect
node-cache is used for temporary in-memory data storage with TTL, not permanent storage or UI.
How do you retrieve a cached value using node-cache?
Acache.set(key)
Bcache.get(key)
Ccache.delete(key)
Dcache.fetch(key)
✗ Incorrect
The get method retrieves the value stored under the given key.
What does TTL stand for in caching?
ATime To Live
BTotal Time Limit
CTemporary Transfer Link
DTimed Token Lock
✗ Incorrect
TTL means Time To Live, the duration data stays valid in cache.
If a cached item expires, what will cache.get(key) return?
Anull
Bempty string
Cfalse
Dundefined
✗ Incorrect
Expired or missing keys return undefined in node-cache.
Which scenario is best suited for using node-cache?
ASaving large video files.
BStoring user passwords permanently.
CCaching API responses to reduce database calls.
DHosting a website's static files.
✗ Incorrect
Caching API responses temporarily speeds up repeated requests without permanent storage.
Explain how you would use node-cache to improve performance in an Express app.
Think about saving repeated data to avoid slow operations.
You got /5 concepts.
What are the limitations of using in-memory caching with node-cache?
Consider what happens when the server stops or restarts.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using node-cache in an Express app?
easy
A. To permanently save data to a database
B. To temporarily store data in memory for faster access
C. To handle user authentication securely
D. To serve static files like images and CSS
Solution
Step 1: Understand what caching means
Caching means storing data temporarily to avoid repeated slow operations.
Step 2: Identify node-cache role in Express
node-cache stores data in memory temporarily to speed up data retrieval.
Final Answer:
To temporarily store data in memory for faster access -> Option B
Quick Check:
In-memory caching = temporary fast storage [OK]
Hint: Cache stores data temporarily to speed up access [OK]
Common Mistakes:
Confusing cache with permanent database storage
Thinking node-cache handles authentication
Assuming node-cache serves static files
2. Which of the following is the correct way to create a new cache instance using node-cache?
easy
A. const cache = new nodeCache();
B. const cache = nodeCache.create();
C. const cache = new NodeCache();
D. const cache = NodeCache();
Solution
Step 1: Recall node-cache import and usage
node-cache is imported as NodeCache and instantiated with new keyword.
Step 2: Check syntax correctness
Correct syntax is: const cache = new NodeCache();
Final Answer:
const cache = new NodeCache(); -> Option C
Quick Check:
Use new NodeCache() to create cache [OK]
Hint: Use 'new' keyword with NodeCache class [OK]
Common Mistakes:
Calling nodeCache.create() which does not exist
Using lowercase 'nodeCache' instead of 'NodeCache'
Omitting 'new' keyword when creating instance
3. Given the code snippet below, what will be the output when accessing /data twice quickly?
D. Missing 'new' keyword when creating cache instance
Solution
Step 1: Check cache instance creation
node-cache requires 'new NodeCache()' to create an instance.
Step 2: Identify missing 'new' keyword
Code uses 'NodeCache()' without 'new', causing error.
Final Answer:
Missing 'new' keyword when creating cache instance -> Option D
Quick Check:
Always use 'new' with NodeCache [OK]
Hint: Remember 'new' keyword for NodeCache instance [OK]
Common Mistakes:
Forgetting 'new' keyword
Thinking cache.set needs callback
Assuming cache keys must be special format
5. You want to cache user profile data for 5 minutes using node-cache. Which code snippet correctly sets the cache with expiration and retrieves it safely in Express?
hard
A. cache.set('user1', profile, 300); const data = cache.get('user1'); if (data) res.send(data); else res.send('No cache');
B. cache.set('user1', profile); const data = cache.get('user1', 300); if (data) res.send(data); else res.send('No cache');
C. cache.set('user1', profile, 5); const data = cache.get('user1'); if (data) res.send(data); else res.send('No cache');
D. cache.set('user1', profile, 300000); const data = cache.get('user1'); if (data) res.send(data); else res.send('No cache');
Solution
Step 1: Understand TTL unit in node-cache
TTL (time to live) is in seconds, so 5 minutes = 300 seconds.
Step 2: Check correct usage of set and get
cache.set(key, value, ttl) sets with expiration; cache.get(key) retrieves value.
Final Answer:
cache.set('user1', profile, 300); const data = cache.get('user1'); if (data) res.send(data); else res.send('No cache'); -> Option A
Quick Check:
TTL in seconds, set with 300 for 5 minutes [OK]
Hint: TTL is seconds; 5 minutes = 300 seconds [OK]
Common Mistakes:
Using TTL in milliseconds instead of seconds
Passing TTL to get() method incorrectly
Setting TTL too low (5 seconds instead of 300)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.