0
0
Expressframework~10 mins

In-memory caching with node-cache in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - In-memory caching with node-cache
Start Express Server
Receive HTTP Request
Check Cache for Key
Return Cached Data
Send Response
End Request
This flow shows how a server checks the cache first, returns cached data if found, or fetches fresh data and caches it before responding.
Execution Sample
Express
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 10 });

app.get('/data', (req, res) => {
  const key = 'myData';
  const cached = cache.get(key);
  if (cached !== undefined) return res.send(cached);
  const freshData = { value: 42 };
  cache.set(key, freshData);
  res.send(freshData);
});
This code checks if 'myData' is cached; if yes, returns it; if no, creates fresh data, caches it, then returns it.
Execution Table
StepActionCache StateResponse SentNotes
1Request '/data' received{}NoCache empty initially
2Check cache for 'myData'{}No'myData' not found
3Create freshData {value:42}{}NoPrepare data to cache
4Set 'myData' in cache{'myData': {value:42}}NoData cached with TTL 10s
5Send freshData in response{'myData': {value:42}}YesResponse sent with fresh data
6Next request '/data' received{'myData': {value:42}}NoCache has data now
7Check cache for 'myData'{'myData': {value:42}}No'myData' found in cache
8Send cached data in response{'myData': {value:42}}YesResponse sent with cached data
9After 10s TTL expires{}NoCache entry expired and removed
💡 Cache returns data if found; otherwise, fresh data is cached and returned. Cache entries expire after TTL.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 9
cache{}{'myData': {value:42}}{'myData': {value:42}}{}
cachedundefinedundefined{value:42}undefined
freshDataundefined{value:42}undefinedundefined
Key Moments - 3 Insights
Why does the server return cached data on the second request?
Because at step 7 in the execution table, the cache contains 'myData', so the server sends cached data without creating fresh data.
What happens when the cache TTL expires?
At step 9, the cache entry for 'myData' is removed automatically, so the cache becomes empty and fresh data will be created on the next request.
Why do we check the cache before fetching fresh data?
Checking the cache first (steps 2 and 7) avoids unnecessary data fetching, improving speed and reducing load.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after step 4?
Aundefined
B{}
C{'myData': {value:42}}
D{'myData': undefined}
💡 Hint
Check the 'Cache State' column at step 4 in the execution table.
At which step does the server send cached data in response?
AStep 5
BStep 8
CStep 7
DStep 3
💡 Hint
Look for 'Send cached data in response' in the 'Action' column.
If the TTL was set to 0 (no expiration), what would happen at step 9?
ACache would still contain 'myData'
BCache would still be empty
CCache would throw an error
DCache would delete 'myData' immediately
💡 Hint
Refer to the 'Cache State' changes and TTL behavior in the variable_tracker.
Concept Snapshot
In-memory caching with node-cache:
- Create cache with TTL (time to live)
- On request, check cache for key
- If found, return cached data
- If not, fetch fresh data, cache it, then return
- Cached data expires after TTL
- Speeds up repeated requests by avoiding repeated data fetching
Full Transcript
This lesson shows how an Express server uses node-cache to store data in memory temporarily. When a request comes in, the server first checks if the data is already cached. If yes, it sends the cached data immediately. If not, it creates fresh data, stores it in the cache, and then sends it. The cache entries expire after a set time (TTL), so old data is removed automatically. This process helps the server respond faster to repeated requests by avoiding repeated data fetching.