The Cache-Control header tells browsers and servers how to store and reuse web data. It helps make websites faster and saves internet data.
Cache-Control header directives in Rest API
Cache-Control: directive1, directive2, ...
Directives are instructions like no-cache or max-age=3600.
Multiple directives are separated by commas.
Cache-Control: no-cache
Cache-Control: max-age=3600Cache-Control: no-store
Cache-Control: public, max-age=86400This simple web server sends a Cache-Control header telling browsers to keep the page for 60 seconds before checking for updates.
from flask import Flask, make_response app = Flask(__name__) @app.route('/') def home(): response = make_response('Hello, world!') response.headers['Cache-Control'] = 'public, max-age=60' return response if __name__ == '__main__': app.run(debug=True)
no-cache means "check with server before using cached data" but still stores it.
no-store means "do not save this data anywhere" for privacy or security.
max-age sets how many seconds the data is fresh and can be reused without checking.
Cache-Control header controls how browsers and servers save and reuse web data.
Use directives like no-cache, no-store, and max-age to set caching rules.
Proper caching makes websites faster and saves data.
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
