Expiration-based caching helps store data temporarily so your app can use it quickly without asking the server every time. It automatically removes old data after a set time to keep things fresh.
Expiration-based caching in Rest API
Cache-Control: max-age=<seconds>
This header tells the browser or client how long to keep the cached data before asking the server again.
max-age is the number of seconds the data is fresh.
Cache-Control: max-age=60Cache-Control: max-age=0, no-cacheCache-Control: max-age=3600, publicThis simple Flask app returns JSON data with a Cache-Control header telling clients to cache the response for 10 seconds. If you request the endpoint multiple times within 10 seconds, the client can use the cached data instead of asking the server again.
from flask import Flask, jsonify, make_response import time app = Flask(__name__) @app.route('/data') def data(): response = make_response(jsonify({'message': 'Hello, world!', 'time': time.time()})) response.headers['Cache-Control'] = 'max-age=10' return response if __name__ == '__main__': app.run(debug=False)
Expiration-based caching works best when data changes predictably or infrequently.
Clients may ignore caching headers, so always validate important data on the server.
Use short expiration times for fast-changing data to avoid showing stale info.
Expiration-based caching stores data temporarily to speed up repeated requests.
Use the Cache-Control: max-age header to set how long data stays fresh.
This method helps reduce server load and improve user experience by avoiding unnecessary data fetching.
Practice
Cache-Control: max-age header do in expiration-based caching?Solution
Step 1: Understand the role of Cache-Control header
The Cache-Control header controls caching behavior for HTTP responses.Step 2: Identify the meaning of max-age directive
The max-age directive specifies the time in seconds that the response is considered fresh and can be served from cache.Final Answer:
It sets how long the cached data is considered fresh. -> Option CQuick Check:
max-age = cache freshness duration [OK]
- Confusing max-age with disabling cache
- Thinking max-age deletes cache immediately
- Assuming max-age forces fresh fetch every time
Solution
Step 1: Recall correct Cache-Control syntax
The Cache-Control header uses directives separated by commas, with max-age specified as max-age=seconds.Step 2: Compare options to correct syntax
Only Cache-Control: max-age=300 uses the correct format: max-age=300.Final Answer:
Cache-Control: max-age=300 -> Option BQuick Check:
Correct header syntax = max-age=seconds [OK]
- Using colon instead of equal sign
- Using incorrect directive names like expire or timeout
- Adding spaces inside the directive
Cache-Control: max-age=10What will happen if a client requests the same resource 5 seconds later?
Solution
Step 1: Understand max-age meaning
max-age=10 means the cached data is fresh for 10 seconds after the first fetch.Step 2: Check time elapsed since caching
5 seconds is less than 10 seconds, so cache is still fresh and valid.Final Answer:
The client will use the cached response without contacting the server. -> Option AQuick Check:
Elapsed time < max-age = use cache [OK]
- Assuming cache expires immediately
- Thinking client always fetches new data
- Confusing max-age with cache invalidation
Cache-Control: max-age=60 but clients still fetch new data every time. What is the likely mistake?Solution
Step 1: Check for conflicting headers
If the server sends a no-cache directive, it forces clients to revalidate or fetch fresh data despite max-age.Step 2: Understand max-age units and client support
max-age is in seconds, and most clients support caching headers, so these are unlikely causes.Final Answer:
The server is sending no-cache header overriding max-age. -> Option DQuick Check:
no-cache overrides max-age [OK]
- Confusing seconds with milliseconds for max-age
- Blaming client support without checking headers
- Assuming URL format affects caching
Solution
Step 1: Understand caching for 5 minutes
max-age=300 sets cache freshness for 300 seconds (5 minutes).Step 2: Ensure fresh data after expiration
must-revalidate forces clients to check with server after max-age expires before using cached data.Step 3: Evaluate other options
no-store disables caching, no-cache forces revalidation every time, max-age=0 means no caching.Final Answer:
Cache-Control: max-age=300, must-revalidate -> Option AQuick Check:
max-age + must-revalidate = cache then fresh after expiry [OK]
- Using no-store disables caching completely
- Using no-cache forces revalidation every request
- Setting max-age=0 disables caching
Ready to prove it?
Pass the test and this pattern moves unknown
