HTTP Cache Header Generator
Visually build Cache-Control headers with directives explained.
Generated Header
Cache-Control: (none)
Presets
Frequently Asked Questions
Code Implementation
from flask import Flask, make_response, send_file
import hashlib
import time
app = Flask(__name__)
@app.route('/static/asset')
def serve_immutable_asset():
"""Cache forever β content-hashed file (e.g., bundle.abc123.js)"""
response = make_response("asset content")
response.headers['Cache-Control'] = 'public, max-age=31536000, immutable'
return response
@app.route('/api/data')
def serve_api_data():
"""Short-lived private API response with ETag"""
data = "dynamic content"
etag = hashlib.md5(data.encode()).hexdigest()
response = make_response(data)
response.headers['Cache-Control'] = 'private, max-age=60'
response.headers['ETag'] = f'"{etag}"'
return response
@app.route('/page')
def serve_page():
"""HTML page: always check freshness, cache 10 minutes"""
response = make_response("<html>...</html>")
response.headers['Cache-Control'] = 'public, max-age=600, must-revalidate'
response.headers['Last-Modified'] = 'Mon, 01 Jan 2024 00:00:00 GMT'
return response
@app.route('/auth')
def serve_auth_page():
"""Never cache sensitive pages"""
response = make_response("sensitive content")
response.headers['Cache-Control'] = 'no-store'
return responseComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.