Skip to main content

Server-side HTTP caching for ASGI applications, inspired by Django's cache framework

Project description


Build Status Coverage Package version

asgi-caches provides middleware and utilities for adding server-side HTTP caching to ASGI applications. It is powered by async-caches, and inspired by Django's cache framework.

Documentation is available at:

Note: this project is in an "alpha" status. Several features still need to be implemented, and you should expect breaking API changes across minor versions.


  • Compatibility with any ASGI application (e.g. Starlette, FastAPI, Quart, etc.).
  • Support for application-wide or per-endpoint caching.
  • Ability to fine-tune the cache behavior (TTL, cache control) down to the endpoint level.
  • Clean and explicit API enabled by a loose coupling with async-caches.
  • Fully type annotated.
  • 100% test coverage.


pip install "asgi-caches==0.*"


from asgi_caches.middleware import CacheMiddleware

cache = Cache("locmem://null")

async def app(scope, receive, send):
    assert scope["type"] == "http"
    headers = [(b"content-type", "text/plain")]
    await send({"type": "http.response.start", "status": 200, "headers": headers})
    await send({"type": "http.response.body", "body": b"Hello, world!"})

app = CacheMiddleware(app, cache=cache)

This example:

  • Sets up an in-memory cache (see the async-caches docs for specifics).
  • Sets up an application (in this case, a raw-ASGI 'Hello, world!' app).
  • Applies caching on the entire application.

To learn more, head to the documentation.


Due credit goes to the Django developers and maintainers, as a lot of the API and implementation was directly inspired by the Django cache framework.




All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog.

0.3.1 - 2019-11-23


  • Disallow applying cache middleware multiple times. (Pull #21)

0.3.0 - 2019-11-12


  • Add @cache_control() decorator and its underlying middleware. (Pull #19)

0.2.0 - 2019-11-12


  • Add @cached() decorator. (Pull #15)

0.1.1 - 2019-11-12


  • Add DEBUG and TRACE logs. (Pull #14)

0.1.0 - 2019-11-12


  • Add CacheMiddleware. (Pull #8)
  • Prevent caching of responses that have cookies when the request has none. (Pull #9)
  • Prevent caching of responses if the cache TTL is zero. (Pull #10)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi-caches-0.3.1.tar.gz (11.7 kB view hashes)

Uploaded source

Built Distribution

asgi_caches-0.3.1-py3-none-any.whl (12.4 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page