📖 Scenario: You are building a simple REST API that returns user profile data. To improve performance and control how clients cache the responses, you need to add Cache-Control headers with specific directives.
🎯 Goal: Build a REST API endpoint that returns a JSON user profile and sets the Cache-Control header with the directive max-age=60 to tell clients to cache the response for 60 seconds.
📋 What You'll Learn
Create a dictionary called user_profile with keys id, name, and email and exact values 1, "Alice", and "alice@example.com" respectively.
Create a variable called cache_control_header and set it to the string "max-age=60".
Create a function called get_user_profile that returns a tuple with user_profile and a dictionary containing the Cache-Control header set to cache_control_header.
Print the result of calling get_user_profile().
💡 Why This Matters
🌍 Real World
APIs use Cache-Control headers to tell browsers and clients how long to keep data before asking again. This helps speed up apps and reduce server load.
💼 Career
Understanding HTTP headers like Cache-Control is important for backend developers, API designers, and anyone working with web services.
Progress0 / 4 steps
1
Create the user profile data
Create a dictionary called user_profile with these exact entries: 'id': 1, 'name': "Alice", and 'email': "alice@example.com".
Rest API
Hint
Use curly braces {} to create a dictionary with the exact keys and values.
2
Set the Cache-Control header directive
Create a variable called cache_control_header and set it to the string "max-age=60".
Rest API
Hint
Assign the exact string "max-age=60" to the variable cache_control_header.
3
Create the API function with Cache-Control header
Create a function called get_user_profile that returns a tuple with user_profile and a dictionary containing the Cache-Control header set to cache_control_header.
Rest API
Hint
Define a function that returns a tuple: the user_profile dictionary and a headers dictionary with the Cache-Control key.
4
Print the API response
Print the result of calling get_user_profile().
Rest API
Hint
Use print(get_user_profile()) to show the returned data and headers.
Practice
(1/5)
1. What does the Cache-Control: no-store directive instruct browsers to do?
easy
A. Not to save any part of the response in cache
B. Allow caching but revalidate before use
C. Cache the response for a specified time
D. Cache the response only if it is public
Solution
Step 1: Understand the meaning of no-store
The no-store directive means the browser must not save any part of the response in cache.
Step 2: Compare with other directives
Unlike no-cache which allows caching but requires revalidation, no-store forbids storing at all.
Final Answer:
Not to save any part of the response in cache -> Option A
Quick Check:
no-store means no saving [OK]
Hint: No-store means do not save anything at all [OK]
Common Mistakes:
Confusing no-store with no-cache
Thinking no-store allows caching with revalidation
Assuming no-store caches for a short time
2. Which of the following is the correct syntax to set a Cache-Control header that caches content for 3600 seconds?
easy
A. Cache-Control: max-age=3600
B. Cache-Control: maxage:3600
C. Cache-Control: max_age=3600
D. Cache-Control: max-age:3600
Solution
Step 1: Recall correct Cache-Control syntax
The correct syntax uses a hyphen and equals sign: max-age=3600.
Step 2: Identify incorrect options
Options with colon or underscore instead of equals or hyphen are invalid syntax.
Final Answer:
Cache-Control: max-age=3600 -> Option A
Quick Check:
Use hyphen and equals for max-age [OK]
Hint: Use hyphen and equals sign for max-age [OK]
Common Mistakes:
Using colon instead of equals
Replacing hyphen with underscore
Omitting equals sign
3. Given the header Cache-Control: public, max-age=600, what will happen when a browser receives this response?
medium
A. The response will be cached privately for 10 minutes
B. The response will not be cached at all
C. The response will be cached and shared by any user for 10 minutes
D. The response will be cached but must be revalidated every time
Solution
Step 1: Understand the public directive
public means the response can be cached by any cache, including shared caches.
Step 2: Interpret max-age=600
This means the cached response is fresh for 600 seconds (10 minutes) without revalidation.
Final Answer:
The response will be cached and shared by any user for 10 minutes -> Option C
Quick Check:
public + max-age=600 means shared cache for 10 minutes [OK]
Hint: Public means shared cache; max-age sets cache time [OK]
Common Mistakes:
Thinking public means private caching
Confusing max-age with no-cache
Assuming revalidation is always required
4. You see this Cache-Control header in a response: Cache-Control: no-cache, max-age=3600. What is wrong with this header?
medium
A. The syntax is invalid because directives cannot be combined
B. The no-cache directive conflicts with max-age and disables caching
C. There is no error; this header caches for 3600 seconds
D. The max-age value must be zero when no-cache is used
Solution
Step 1: Understand no-cache directive
no-cache means the cache must revalidate before using the cached response.
Step 2: Analyze conflict with max-age=3600
max-age=3600 says the response is fresh for 3600 seconds, but no-cache forces revalidation, effectively disabling caching without validation.
Final Answer:
The no-cache directive conflicts with max-age and disables caching -> Option B