Bird
Raised Fist0
Expressframework~20 mins

In-memory caching with node-cache in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Node-Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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}`);
});
ABoth calls return 'Cached: fresh-data'
BFirst call: 'Cached: fresh-data', second call: 'New: fresh-data'
CBoth calls return 'New: fresh-data'
DFirst call: 'New: fresh-data', second call: 'Cached: fresh-data'
Attempts:
2 left
💡 Hint
Think about what happens when the cache is empty and then filled.
❓ state_output
intermediate
2: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);
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Remember the TTL and when keys expire.
🔧 Debug
advanced
2: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');
});
ABecause the key 'item' is misspelled in cache.get compared to cache.set.
BBecause cache.get returns undefined if the key is not found, so the first call returns 'Cache miss' but subsequent calls return 'Cached: value'.
CBecause the cache instance is recreated on every request, so the cache is always empty.
DBecause cache.set is asynchronous and the value is not set before cache.get runs.
Attempts:
2 left
💡 Hint
Check if the cache instance is reused and if set/get are synchronous.
📝 Syntax
advanced
2: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?
Acache.set('user', 'Alice', ttl=10);
Bcache.set('user', 'Alice', { ttl: 10 });
Ccache.set('user', 'Alice', 10);
Dcache.set('user', 'Alice', ttl: 10);
Attempts:
2 left
💡 Hint
Check the node-cache set method signature for TTL parameter.
🧠 Conceptual
expert
2: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?
AThe key is stored permanently and never expires unless deleted manually.
BThe key expires immediately and is removed from the cache.
CSetting TTL 0 causes a runtime error.
DThe key is stored with the default TTL set in the cache constructor.
Attempts:
2 left
💡 Hint
Think about what TTL 0 means in caching terms.

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

  1. Step 1: Understand what caching means

    Caching means storing data temporarily to avoid repeated slow operations.
  2. Step 2: Identify node-cache role in Express

    node-cache stores data in memory temporarily to speed up data retrieval.
  3. Final Answer:

    To temporarily store data in memory for faster access -> Option B
  4. 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

  1. Step 1: Recall node-cache import and usage

    node-cache is imported as NodeCache and instantiated with new keyword.
  2. Step 2: Check syntax correctness

    Correct syntax is: const cache = new NodeCache();
  3. Final Answer:

    const cache = new NodeCache(); -> Option C
  4. 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?
const express = require('express');
const NodeCache = require('node-cache');
const app = express();
const cache = new NodeCache();

app.get('/data', (req, res) => {
  const cached = cache.get('myData');
  if (cached) {
    res.send('From cache: ' + cached);
  } else {
    const freshData = 'Hello World';
    cache.set('myData', freshData, 10);
    res.send('Fresh data: ' + freshData);
  }
});

app.listen(3000);
medium
A. First request: Fresh data: Hello World; Second request: From cache: Hello World
B. Both requests: Fresh data: Hello World
C. Both requests: From cache: Hello World
D. First request: From cache: Hello World; Second request: Fresh data: Hello World

Solution

  1. Step 1: Analyze cache check on first request

    Cache is empty initially, so first request sets and returns fresh data.
  2. Step 2: Analyze cache check on second request

    Second request finds data in cache and returns cached data.
  3. Final Answer:

    First request: Fresh data: Hello World; Second request: From cache: Hello World -> Option A
  4. Quick Check:

    Cache empty first, then hit cache second [OK]
Hint: First request sets cache, second reads from cache [OK]
Common Mistakes:
  • Assuming cache is always empty
  • Thinking cache returns fresh data every time
  • Mixing order of fresh and cached responses
4. Identify the error in this code snippet using node-cache in Express:
const NodeCache = require('node-cache');
const cache = NodeCache();

app.get('/item', (req, res) => {
  const item = cache.get('itemKey');
  if (!item) {
    cache.set('itemKey', 'value');
    res.send('Set new item');
  } else {
    res.send('Cached item: ' + item);
  }
});
medium
A. Cache key 'itemKey' is invalid
B. Express app is not imported
C. cache.set requires a callback function
D. Missing 'new' keyword when creating cache instance

Solution

  1. Step 1: Check cache instance creation

    node-cache requires 'new NodeCache()' to create an instance.
  2. Step 2: Identify missing 'new' keyword

    Code uses 'NodeCache()' without 'new', causing error.
  3. Final Answer:

    Missing 'new' keyword when creating cache instance -> Option D
  4. 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

  1. Step 1: Understand TTL unit in node-cache

    TTL (time to live) is in seconds, so 5 minutes = 300 seconds.
  2. Step 2: Check correct usage of set and get

    cache.set(key, value, ttl) sets with expiration; cache.get(key) retrieves value.
  3. Final Answer:

    cache.set('user1', profile, 300); const data = cache.get('user1'); if (data) res.send(data); else res.send('No cache'); -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard