Cache-Control header directives in Rest API - Time & Space Complexity
When working with Cache-Control headers in REST APIs, it's important to understand how the processing time changes as the number of directives grows.
We want to know how the time to parse and apply these directives scales with input size.
Analyze the time complexity of the following code snippet.
function parseCacheControl(header) {
const directives = header.split(",");
const result = {};
for (const directive of directives) {
const [key, value] = directive.trim().split("=");
result[key] = value || true;
}
return result;
}
This code splits the Cache-Control header string into directives and parses each into a key-value pair.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each directive in the header string.
- How many times: Once for each directive found after splitting by commas.
The time to parse grows directly with the number of directives in the header.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loops and splits |
| 100 | About 100 loops and splits |
| 1000 | About 1000 loops and splits |
Pattern observation: The work increases evenly as the number of directives increases.
Time Complexity: O(n)
This means the time to parse Cache-Control directives grows linearly with the number of directives.
[X] Wrong: "Parsing Cache-Control headers takes constant time no matter how many directives there are."
[OK] Correct: Each directive must be processed individually, so more directives mean more work.
Understanding how parsing headers scales helps you write efficient API code and shows you can think about performance in real-world tasks.
"What if the header string was already split into an array before parsing? How would that affect the time complexity?"
Practice
Cache-Control: no-store directive instruct browsers to do?Solution
Step 1: Understand the meaning of
Theno-storeno-storedirective means the browser must not save any part of the response in cache.Step 2: Compare with other directives
Unlikeno-cachewhich allows caching but requires revalidation,no-storeforbids storing at all.Final Answer:
Not to save any part of the response in cache -> Option AQuick Check:
no-storemeans no saving [OK]
- Confusing no-store with no-cache
- Thinking no-store allows caching with revalidation
- Assuming no-store caches for a short time
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 AQuick Check:
Use hyphen and equals for max-age [OK]
- Using colon instead of equals
- Replacing hyphen with underscore
- Omitting equals sign
Cache-Control: public, max-age=600, what will happen when a browser receives this response?Solution
Step 1: Understand the
publicdirectivepublicmeans the response can be cached by any cache, including shared caches.Step 2: Interpret
This means the cached response is fresh for 600 seconds (10 minutes) without revalidation.max-age=600Final Answer:
The response will be cached and shared by any user for 10 minutes -> Option CQuick Check:
public + max-age=600means shared cache for 10 minutes [OK]
- Thinking public means private caching
- Confusing max-age with no-cache
- Assuming revalidation is always required
Cache-Control: no-cache, max-age=3600. What is wrong with this header?Solution
Step 1: Understand
no-cachedirectiveno-cachemeans the cache must revalidate before using the cached response.Step 2: Analyze conflict with
max-age=3600max-age=3600says the response is fresh for 3600 seconds, butno-cacheforces revalidation, effectively disabling caching without validation.Final Answer:
Theno-cachedirective conflicts withmax-ageand disables caching -> Option BQuick Check:
no-cacheoverrides max-age freshness [OK]
- Thinking directives cannot be combined
- Assuming max-age always overrides no-cache
- Believing no-cache disables caching completely
Solution
Step 1: Identify directive that prevents any storage
no-storetells browsers and caches not to store the response anywhere.Step 2: Compare with other options
no-cacheallows storing but requires revalidation;privateallows browser-only caching;publicallows shared caching.Final Answer:
Cache-Control: no-store -> Option DQuick Check:
no-storeforbids all storage [OK]
- Using no-cache instead of no-store
- Assuming private disables all caching
- Setting max-age=0 without no-store
Ready to prove it?
Pass the test and this pattern moves unknown
