Skip to main content

Middleware for managing HTTP cache headers and client-side caching behavior.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_cachecontrol


Swarmauri Middleware Cachecontrol

Middleware for managing HTTP cache headers and client-side caching behavior.

Features

  • Configurable max_age that controls how long clients may cache responses.
  • Toggle caching on or off at runtime with the enabled flag.
  • Adds Cache-Control, timestamp-based ETag, and Vary: Accept-Encoding headers to successful responses.
  • Inspects If-Modified-Since and If-None-Match request headers to short-circuit unchanged responses with 304 Not Modified.

Installation

Install the package with your preferred Python packaging tool:

pip install swarmauri_middleware_cachecontrol
poetry add swarmauri_middleware_cachecontrol
uv pip install swarmauri_middleware_cachecontrol

Usage

CacheControlMiddleware accepts two primary configuration options:

  • max_age: Maximum cache lifetime (in seconds) communicated to clients. Defaults to 3600.
  • enabled: When False, the middleware skips all cache headers and lets responses pass through untouched. Defaults to True.

When enabled, the middleware injects cache headers into outgoing responses and mirrors conditional request headers to return 304 Not Modified when the client already has fresh content. Integrate it with FastAPI or Starlette by instantiating the middleware once and delegating to its dispatch method from an @app.middleware("http") handler.

Example

The snippet below shows how to register the middleware with FastAPI and inspect the generated headers using TestClient:

from fastapi import FastAPI, Request
from fastapi.testclient import TestClient

from swarmauri_middleware_cachecontrol import CacheControlMiddleware

app = FastAPI()
cache_control = CacheControlMiddleware(max_age=60)


@app.middleware("http")
async def apply_cache_control(request: Request, call_next):
    return await cache_control.dispatch(request, call_next)


@app.get("/status")
def status() -> dict[str, str]:
    return {"state": "fresh"}


def main() -> None:
    client = TestClient(app)
    response = client.get("/status")
    response.raise_for_status()

    print("Cache-Control:", response.headers["Cache-Control"])
    print("ETag:", response.headers["ETag"])
    print("Vary:", response.headers["Vary"])
    print("Payload:", response.json())


if __name__ == "__main__":
    main()

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

Built Distribution

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

File details

Details for the file swarmauri_middleware_cachecontrol-0.8.0.dev47.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_cachecontrol-0.8.0.dev47.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_cachecontrol-0.8.0.dev47.tar.gz
Algorithm Hash digest
SHA256 1c14447845dfe95c3d4f555dea7676237570ae1eb0c7dba0c0a75c8ac6041cbf
MD5 eb0b1b28e0b4367f7eb14a9659f51413
BLAKE2b-256 a60bf94ff2632bf4f17b9231f5c048cdbc15ca8bfc416ed410861e53191b3fc2

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_cachecontrol-0.8.0.dev47-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_cachecontrol-0.8.0.dev47-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_cachecontrol-0.8.0.dev47-py3-none-any.whl
Algorithm Hash digest
SHA256 b9193611752e9bb29fad560c2b54e81f9a98712efd7c6f1951949ee8161b62d8
MD5 63c93fae14f154eca0dd58a4a0209add
BLAKE2b-256 9614d322dee47d933442a3fb0788211a8cc59d4850bec195fd9a6add4172bfc5

See more details on using hashes here.

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