Skip to main content

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

Project description

starlette-caches

Build Status Coverage Package version

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

Documentation is available at: https://mattmess1221.github.io/starlette-caches/

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 aiocache.
  • Fully type annotated.
  • 100% test coverage.

Installation

pip install "starlette-caches"

To install with redis or memcached support, use:

pip install "starlette-caches[redis,memcached]"

Quickstart

from aiocache import Cache
from starlette_caches.middleware import CacheMiddleware

cache = Cache(ttl=2 * 60)

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 aiocache 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

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

starlette_caches-0.4.0.tar.gz (96.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_caches-0.4.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file starlette_caches-0.4.0.tar.gz.

File metadata

  • Download URL: starlette_caches-0.4.0.tar.gz
  • Upload date:
  • Size: 96.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for starlette_caches-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ee1e46727d5b7b6e20d55a67723c18135df5d8a812708d0ad70d07188bf3a033
MD5 f9e3b47aee76e1185ce83f19ef77916f
BLAKE2b-256 d6c7b8e80fb4e9a7bd25282d70382505d1f3cec0d5d6e26184e11ed532f0a920

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_caches-0.4.0.tar.gz:

Publisher: release.yml on mattmess1221/starlette-caches

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file starlette_caches-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_caches-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8bca9d4e6ae4c2600130b5dd72ac49465fbac4845b6000b7fb61b4102aa0a3
MD5 853f41e49709edf5b69b6578adfc9c3c
BLAKE2b-256 b296e163966084d142138c214504aaebb30fe661271b2e79be85102b23f78e05

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_caches-0.4.0-py3-none-any.whl:

Publisher: release.yml on mattmess1221/starlette-caches

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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