0
0
Expressframework~30 mins

Why caching improves performance in Express - See It in Action

Choose your learning style9 modes available
Why caching improves performance
📖 Scenario: You are building a simple Express server that returns user profile data. To make the server faster, you want to add caching so repeated requests for the same user do not need to fetch data again.
🎯 Goal: Build an Express server that caches user profile data in memory to improve response speed for repeated requests.
📋 What You'll Learn
Create an object called userProfiles with 3 user profiles and their details
Create a cache object called cache to store fetched profiles
Write a function getUserProfile that checks the cache before returning a profile
Add an Express route /user/:id that uses getUserProfile to send the profile data
💡 Why This Matters
🌍 Real World
Caching is used in web servers to speed up responses by storing data temporarily, reducing database or API calls.
💼 Career
Understanding caching helps backend developers optimize performance and scalability of web applications.
Progress0 / 4 steps
1
Create user profile data
Create an object called userProfiles with these exact entries: 1 with name 'Alice' and age 30, 2 with name 'Bob' and age 25, and 3 with name 'Charlie' and age 35.
Express
Need a hint?

Use an object with keys 1, 2, and 3. Each key maps to an object with name and age.

2
Add a cache object
Create an empty object called cache to store cached user profiles.
Express
Need a hint?

Just create an empty object named cache.

3
Write the caching function
Write a function called getUserProfile that takes id as a parameter. It should check if cache[id] exists and return it if yes. Otherwise, get the profile from userProfiles[id], store it in cache[id], and return it.
Express
Need a hint?

Use an if to check cache, else get from userProfiles and save to cache.

4
Add Express route using caching
Add an Express route /user/:id that uses getUserProfile to get the profile for req.params.id and sends it as JSON response. Use express() to create the app and listen on port 3000.
Express
Need a hint?

Import Express, create app, add route with app.get, use getUserProfile, send JSON, and listen on port 3000.