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.1.tar.gz (96.4 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.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for starlette_caches-0.4.1.tar.gz
Algorithm Hash digest
SHA256 668370885d5efe71e4f55f60c91035516f64d003fb5a04abbb0910e5e052416e
MD5 58faaf85933e3e29a8073d5e3ad2cdae
BLAKE2b-256 e96ec91ed981927e79d479c5c1aef9b68bbf9fa838ac312f2bcf5b8c3b15abd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_caches-0.4.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_caches-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1909e80ded720cf0e37a084b3b2a8da0f7c26c24c2d82c9079b32fc038e34cf
MD5 570f4cde2f556b5482c2cc97cc24c81c
BLAKE2b-256 5010f2f0a3664f0e9a2014ab9940eb39124f7785b83ce634e6cf8bc2a0e7502d

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_caches-0.4.1-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