Skip to main content

Flask-CacheControl

Description

A light-weight library to conveniently set Cache-Control headers on the response. Decorate view functions with cache_for, cache, or dont_cache decorators. Makes use of Flask response.cache_control.

This extension does not provide any caching of its own. Its sole purpose is to set Cache-Control and related HTTP headers on the response, so that clients, intermediary proxies or reverse proxies in your jurisdiction which evaluate Cache-Control headers, such as Varnish Cache, do the caching for you.

By default, Cache-Control headers are only appended in case of a successful response (status code 2xx). This behaviour can be controlled with the only_if argument to cache_for and cache decorators. Included options are Always, ResponseIsSuccessful, ResponseIsSuccessfulOrRedirect. Custom behaviour can be implemented by subclassing OnlyIfEvaluatorBase.

If the vary keyword argument is given to cache_for or cache decorators, the Vary HTTP header is returned with the response. Vary headers are appended independent of response status code.

Example

from flask import Flask, render_template
from flask_cachecontrol import (
    cache,
    cache_for,
    dont_cache,
    Always, 
    ResponseIsSuccessfulOrRedirect)


app = Flask(__name__)


@app.route('/')
@cache_for(hours=3)
def index_view():
    return render_template('index_template')

@app.route('/users')
@cache_for(minutes=5, only_if=ResponseIsSuccessfulOrRedirect)
def users_view():
    return render_template('user_template')

@app.route('/stats')
@cache(max_age=3600, public=True, only_if=Always, vary=['User-Agent', 'Referer'])
def stats_view():
    return render_template('stats_template')

@app.route('/dashboard')
@dont_cache()
def dashboard_view():
    return render_template('dashboard_template')

Changelog

0.3.0

  • Add only_if evaluator for successful or redirect (2xx, 3xx) responses (#7)
  • Support Vary-headers (#6)
  • Improve instantiation of callbacks and registry provider
  • BREAKING: Simplify instantiation and hooking into flask response handling (#8)
    • No more need to instantiate FlaskCacheControl for Flask app.
  • BREAKING: Drop support for only_if=None
    • Use more explicit only_if=Always instead
  • BREAKING: Restructure modules
    • Direct imports from modules inside the package need to be adapted.
  • Improve test structuring
  • Fix flask instantiation and import in example

v0.2.1

  • Fix import statement in example

v0.2.0

  • Add tests
  • BREAKING: By default, cache control headers are only applied to successful requests. (status code 2xx) This behaviour can be customized by providing only_if= as a kw to all caching decorators.
  • BREAKING: Requires python 3.3 or higher

Release files for Flask-CacheControl 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-CacheControl 0.3.0
File Size Uploaded
Flask-CacheControl-0.3.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-CacheControl 0.3.0
File Interpreter ABI Platform
Flask_CacheControl-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / Flask-CacheControl-0.3.0.tar.gz

Download URL Flask-CacheControl-0.3.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
39d10ce9dac835f926035b71eec5c8711b2625ecb27dd9b43f09a16ee5235e4e
BLAKE2b-256 checksum
How to use checksums
82e6263fadac78f426428bc9705694069955a3c27b8f1d5b318ed23641d68d0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / Flask_CacheControl-0.3.0-py3-none-any.whl

Download URL Flask_CacheControl-0.3.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d75f56d8f9288d13758da69e7af4c4d9adcd87e7e5db5f57124c6f326e7c89c
BLAKE2b-256 checksum
How to use checksums
c17217ea30f6c30d8181601765942cc0869151d2731c0b07a02aca01bccb520a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page