Skip to content

mattmess1221/starlette-caches

 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Languages

  • Python 98.8%
  • Shell 1.2%