Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

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

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.

Metadata

Release files for swarmauri_middleware_cachecontrol 0.11.0.dev1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swarmauri_middleware_cachecontrol 0.11.0.dev1
File Size Uploaded
swarmauri_middleware_cachecontrol-0.11.0.dev1.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_middleware_cachecontrol 0.11.0.dev1
File Interpreter ABI Platform
swarmauri_middleware_cachecontrol-0.11.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / swarmauri_middleware_cachecontrol-0.11.0.dev1.tar.gz

Download URL swarmauri_middleware_cachecontrol-0.11.0.dev1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7cf84af2f29519ef266563bef907af8faea0e45b727ee63fbc5e51828a246ebf
BLAKE2b-256 checksum
How to use checksums
f7c0b4f80d5d9ac32be1ad53f7cf8acc242c1ee2e98cbe77b9f7b73235590913
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}

Release files / swarmauri_middleware_cachecontrol-0.11.0.dev1-py3-none-any.whl

Download URL swarmauri_middleware_cachecontrol-0.11.0.dev1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
84fc3241c589f2b352290152dc765b053c6b8bd38044bbda757055e2433f4e08
BLAKE2b-256 checksum
How to use checksums
34cc737e54a6a8d68d1348756e47df24bff5838c1969ec33159df86cbe6eeb6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page