Skip to main content

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

Project description

asgi-caches

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: https://asgi-caches.florimond.dev

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.

Features

  • 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.

Installation

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

Quickstart

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.

Credits

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.

License

MIT

Changelog

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

Changed

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

0.3.0 - 2019-11-12

Added

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

0.2.0 - 2019-11-12

Added

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

0.1.1 - 2019-11-12

Added

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

0.1.0 - 2019-11-12

Added

  • 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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file asgi-caches-0.3.1.tar.gz.

File metadata

  • Download URL: asgi-caches-0.3.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for asgi-caches-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b62690a077dfa23ace2cf6d177b717adb924d2aa448406744eca661e5f10fffc
MD5 9d545fd3aca1999c8a1aad009b139eb3
BLAKE2b-256 b7deb7591777802d20d77c5ac592d208b139c597188a57ea5b205c71d0372d42

See more details on using hashes here.

File details

Details for the file asgi_caches-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: asgi_caches-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for asgi_caches-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2e8cbdb7dc49e6fc2ff14aefe0ef9e0808ac624d960586cd9bf974efee3e55f
MD5 957b169d61f71b23027d2c4cfb18e9a4
BLAKE2b-256 1750f6c4cb156812d18bf2ee24f4ccc9cc9a98df8cdba174de30035f8a6c955b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page