0
0
Rest APIprogramming~30 mins

Why caching reduces server load in Rest API - See It in Action

Choose your learning style9 modes available
Why caching reduces server load
📖 Scenario: You are building a simple REST API server that returns user profile data. To make the server faster and reduce the work it does, you want to use caching.
🎯 Goal: Build a simple REST API that caches user data to reduce server load by avoiding repeated data fetching.
📋 What You'll Learn
Create a dictionary called user_data with exact user profiles
Create a variable called cache to store cached results
Write a function called get_user_profile that checks cache before fetching from user_data
Print the profile data for user "alice" twice to show caching in action
💡 Why This Matters
🌍 Real World
Caching is used in real web servers to speed up responses and reduce the work the server must do for repeated requests.
💼 Career
Understanding caching helps backend developers optimize APIs and improve user experience by making apps faster and more efficient.
Progress0 / 4 steps
1
DATA SETUP: Create user data dictionary
Create a dictionary called user_data with these exact entries: 'alice': {'age': 30, 'city': 'New York'}, 'bob': {'age': 25, 'city': 'Los Angeles'}, and 'carol': {'age': 27, 'city': 'Chicago'}.
Rest API
Need a hint?

Use curly braces to create a dictionary with keys 'alice', 'bob', and 'carol'. Each key maps to another dictionary with keys 'age' and 'city'.

2
CONFIGURATION: Create cache dictionary
Create an empty dictionary called cache to store cached user profiles.
Rest API
Need a hint?

Use empty curly braces to create an empty dictionary called cache.

3
CORE LOGIC: Write caching function
Write a function called get_user_profile that takes a parameter username. Inside the function, first check if username is in cache. If yes, return the cached data. Otherwise, get the data from user_data, store it in cache, then return it.
Rest API
Need a hint?

Use if username in cache to check cache. Use user_data.get(username) to get data if not cached.

4
OUTPUT: Print cached results
Print the result of calling get_user_profile with 'alice' twice to show caching reduces repeated data fetching.
Rest API
Need a hint?

Use print(get_user_profile('alice')) twice to show the cached data is returned both times.