Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/scripts/start-moto-s3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
# Start the CI S3 emulator (moto in server mode) on http://127.0.0.1:5000 as
# container `moto-s3`, storing objects in memory. Point a client at it with
# endpoint_url=http://127.0.0.1:5000 and any credentials: moto checks none.
# The image is pinned by digest, so the registry only carries the bytes: the
# daemon verifies them against the digest wherever they come from. Each
# registry's anonymous quota is per source IP and hosted runners share IPs, so
# the pull walks MOTO_REPOS in order, twice, and uses the first that serves it.
#
# CI: bash .github/scripts/start-moto-s3.sh
# Local: bash .github/scripts/start-moto-s3.sh (needs docker and a free port 5000)
# Test: put a stub `docker` first on PATH; MOTO_DIGEST / MOTO_REPOS override the pin.
set -u

# motoserver/moto 5.2.6 multi-arch index, resolved 2026-12-03.
# Every registry listed serves this digest. Renovate does not bump it.
MOTO_DIGEST=${MOTO_DIGEST:-sha256:344e076f31ad1f5ff1ec4492f741e7d42911d1c7a1ef9b1590f56c340b968513}
MOTO_REPOS=${MOTO_REPOS:-mirror.gcr.io/motoserver/moto ghcr.io/getmoto/motoserver docker.io/motoserver/moto}

image=
for round in 1 2; do
for repo in $MOTO_REPOS; do
if timeout 120 docker pull "$repo@$MOTO_DIGEST"; then image=$repo@$MOTO_DIGEST; break 2; fi
done
[ "$round" = 1 ] && sleep 15
done
[ -n "$image" ] || { echo "::error::no registry served moto@${MOTO_DIGEST} (tried: ${MOTO_REPOS})"; exit 1; }

docker run -d --name moto-s3 -p 5000:5000 "$image" || exit 1

# Ready when the server answers its own API on the published port. The 30s
# deadline covers the probes too, so a server that never comes up, or accepts
# but never answers, fails this step instead of hanging the job.
deadline=$((SECONDS + 30))
while [ "$SECONDS" -lt "$deadline" ]; do
if curl --silent --fail --max-time 2 --output /dev/null http://127.0.0.1:5000/moto-api/; then
exit 0
fi
sleep 1
done
echo "::error::moto did not answer on 127.0.0.1:5000 within 30s"
docker logs moto-s3
exit 1
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ jobs:
if: github.event_name == 'push'
run: bash .github/scripts/start-fake-gcs.sh

# Push runs only: tests/integration/test_s3_integration.py skips its whole module when
# nothing answers on 127.0.0.1:5000.
- name: Start the S3 emulator
if: github.event_name == 'push'
run: bash .github/scripts/start-moto-s3.sh

- name: Run full test suite (main)
if: github.event_name == 'push'
env:
Expand Down Expand Up @@ -196,6 +202,10 @@ jobs:
if: github.event_name == 'push'
run: python3 .github/scripts/assert-junit-ran.py junit.xml tests.integration.test_gcs_integration

- name: Check the S3 integration tests ran
if: github.event_name == 'push'
run: python3 .github/scripts/assert-junit-ran.py junit.xml tests.integration.test_s3_integration

# Executable docs are tests: docstring examples, tests/docs/ and the docs/
# markdown blocks. None of them sit under tests/unit or tests/critical, so
# the runs above never collect them.
Expand Down
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_cached_function():

```bash
# Backend selection: set exactly ONE of CACHEKIT_REDIS_URL, CACHEKIT_API_KEY,
# CACHEKIT_MEMCACHED_SERVERS, CACHEKIT_FILE_CACHE_DIR, CACHEKIT_GCS_BUCKET. Two or more is a ConfigurationError at
# CACHEKIT_MEMCACHED_SERVERS, CACHEKIT_FILE_CACHE_DIR, CACHEKIT_GCS_BUCKET, CACHEKIT_S3_BUCKET. Two or more is a ConfigurationError at
# first call, and decorators relying on env auto-detection run uncached (docs/backends/README.md).
# REDIS_URL is only a fallback and never conflicts. With none of them set, decorators cache
# L1-only, in process (encrypted decorators still use localhost Redis; interop decorators refuse
Expand All @@ -252,6 +252,13 @@ CACHEKIT_MEMCACHED_KEY_PREFIX="myapp:" # Default: "" (none)
CACHEKIT_GCS_PREFIX="cachekit/" # Default: "" (bucket root)
CACHEKIT_GCS_TIMEOUT=10.0 # Default: 10.0 seconds

# S3 Backend (optional: pip install cachekit[s3]; boto3's default credential chain)
# CACHEKIT_S3_BUCKET="my-cache-bucket" # Required to select it
CACHEKIT_S3_PREFIX="cachekit/" # Default: "" (bucket root)
CACHEKIT_S3_REGION="eu-west-2" # Default: none (boto3's own lookup)
# CACHEKIT_S3_ENDPOINT_URL="https://s3.example.com" # Default: none (AWS S3); set for an S3-compatible store
CACHEKIT_S3_TIMEOUT=10.0 # Default: 10.0 seconds

# Optional Configuration
CACHEKIT_MAX_VALUE_SIZE=104857600
CACHEKIT_ARROW_COMPRESSION=zstd
Expand All @@ -270,6 +277,7 @@ backend that fits your infrastructure — they're peers behind the same `@cache`
| CacheKit Cloud | Managed, zero-ops (closed beta) | `CACHEKIT_API_KEY` |
| Memcached | High-throughput, existing infra | `CACHEKIT_MEMCACHED_SERVERS` |
| Google Cloud Storage | Durable results on Cloud Run and jobs, no server | `CACHEKIT_GCS_BUCKET` |
| S3 | Durable results on Lambda, ECS tasks and batch jobs, no server; or an S3-compatible store | `CACHEKIT_S3_BUCKET` |
| File / L1-only | Local dev, tests, no external deps | `CACHEKIT_FILE_CACHE_DIR` / nothing (or `backend=None`) |

```bash
Expand Down Expand Up @@ -361,6 +369,28 @@ One object per entry, in the file backend's format. See [Google Cloud Storage Ba

</details>

<details>
<summary><strong>S3 — Optional</strong></summary>

```python notest
from cachekit import cache
from cachekit.backends.s3 import S3Backend, S3BackendConfig

# pip install cachekit[s3]

backend = S3Backend(S3BackendConfig(bucket="my-cache-bucket")) # boto3's default credential chain

@cache(backend=backend, ttl=3600)
def expensive_api_call(user_id: int):
return fetch_user_data(user_id)
```

One object per entry, in the file backend's format, in Amazon S3 or an S3-compatible store (`endpoint_url`). See [S3 Backend](docs/backends/s3.md) for IAM and the bucket lifecycle rule.

*Requires: `pip install cachekit[s3]` or `uv add cachekit[s3]`*

</details>

> **CacheKit Cloud (Closed Beta)**
> Managed caching with zero infrastructure. L1+L2 caching, circuit breaker, and automatic failover — no Redis to manage.
> *CacheKit Cloud is in closed beta: sign-up is by waitlist at [dash.cachekit.io](https://dash.cachekit.io).*
Expand Down Expand Up @@ -401,7 +431,7 @@ See [SECURITY.md][security-url] for vulnerability reporting and detailed documen
- Connection pooling with thread affinity
- The CacheKit Cloud backend keeps idle connections pooled until the server closes them, with TCP keepalive probes, so a request after a pause skips a new TLS handshake
- Distributed locking guards against cache stampedes across pods, best effort (async functions, on a backend that supports it)
- Pluggable backend abstraction (Redis, CacheKit Cloud, File, Memcached, Google Cloud Storage, custom)
- Pluggable backend abstraction (Redis, CacheKit Cloud, File, Memcached, Google Cloud Storage, S3, custom)
- Untrusted-decode bounds: nesting depth and header-declared allocation are capped on every cache read (a forged entry is a bounded cache miss), verified against the protocol's shared [`decode-bounds.json`](https://github.com/cachekit-io/protocol/blob/42ea601f3c1656df0a06904afa2406604f43f57d/test-vectors/decode-bounds.json) vectors

> [!NOTE]
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Choose your storage backend:
| [File](backends/file.md) | Local filesystem, zero dependencies |
| [Memcached](backends/memcached.md) | High-throughput, consistent hashing |
| [Google Cloud Storage](backends/gcs.md) | Durable, one object per entry, no server |
| [S3](backends/s3.md) | Durable, one object per entry, no server; Amazon S3 and S3-compatible stores |
| [CacheKit Cloud](backends/cachekitio.md) | Managed, zero infrastructure |
| [L1-Only (None)](backends/none.md) | In-memory only, no external services |
| [Custom](backends/custom.md) | Implement your own backend |
Expand Down Expand Up @@ -92,6 +93,7 @@ docs/
│ ├── redis.md, file.md # Built-in backends
│ ├── memcached.md, cachekitio.md # Optional backends
│ ├── gcs.md # Google Cloud Storage (optional)
│ ├── s3.md # Amazon S3 and S3-compatible stores (optional)
│ ├── none.md # L1-only mode
│ └── custom.md # Custom backend guide
│
Expand Down
2 changes: 1 addition & 1 deletion docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ For error examples and handling patterns, see [Troubleshooting Guide](troublesho

## Backend Abstraction

cachekit uses a protocol-based backend abstraction (PEP 544) that allows pluggable storage backends for L2 cache. Built-in backends include Redis, CacheKit Cloud, File, Memcached, and Google Cloud Storage. You can also implement custom backends for any key-value store.
cachekit uses a protocol-based backend abstraction (PEP 544) that allows pluggable storage backends for L2 cache. Built-in backends include Redis, CacheKit Cloud, File, Memcached, Google Cloud Storage, and S3. You can also implement custom backends for any key-value store.

For comprehensive backend guide with examples and implementation patterns, see **[Backend Guide](backends/README.md)**.

Expand Down
18 changes: 15 additions & 3 deletions docs/backends/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Backend Guide

Pluggable L2 cache storage for cachekit. Five backends are included out of the box — Redis, File (local), Memcached, Google Cloud Storage, and CacheKit Cloud (managed). With no backend configured, a plaintext decorator runs in [L1-only mode](none.md), as `backend=None` does; encrypted and interop decorators still need a backend ([Nothing Configured](#4-nothing-configured-l1-only)). You can also implement custom backends for any key-value store.
Pluggable L2 cache storage for cachekit. Six backends are included out of the box — Redis, File (local), Memcached, Google Cloud Storage, S3, and CacheKit Cloud (managed). With no backend configured, a plaintext decorator runs in [L1-only mode](none.md), as `backend=None` does; encrypted and interop decorators still need a backend ([Nothing Configured](#4-nothing-configured-l1-only)). You can also implement custom backends for any key-value store.

## Overview

Expand Down Expand Up @@ -99,6 +99,7 @@ class BaseBackend(Protocol):
| **[Redis](redis.md)** | One network round trip per operation ([not measured here](../performance.md#not-measured-here)) | Yes (RDB/AOF) | Yes | Yes | Yes |
| **[Memcached](memcached.md)** | One network round trip per operation ([not measured here](../performance.md#not-measured-here)) | No | Yes | Yes (max 30d) | No |
| **[Google Cloud Storage](gcs.md)** | One HTTPS request to GCS per ordinary operation ([request counts](gcs.md#requests-per-operation); [not measured here](../performance.md#not-measured-here)) | Yes | Yes | Yes (checked on read) | No |
| **[S3](s3.md)** | One HTTPS request to S3 per operation ([request counts](s3.md#requests-per-operation); [not measured here](../performance.md#not-measured-here)) | Yes | Yes | Yes (checked on read) | No |
| **[CacheKit Cloud](cachekitio.md)** | 42–45ms p50 hit, measured from MEL ([varies by vantage](cachekitio.md#characteristics)) | Yes | Yes | Yes | Yes |
| **[HTTP (custom)](custom.md)** | One HTTP request per operation ([not measured here](../performance.md#not-measured-here)) | Varies | Yes | Varies | Varies |
| **[DynamoDB (custom)](custom.md)** | One DynamoDB API request per operation ([not measured here](../performance.md#not-measured-here)) | Yes | Yes | Yes | No |
Expand All @@ -109,6 +110,8 @@ class BaseBackend(Protocol):
`refresh_ttl_on_get` does not apply to it (see [Memcached](memcached.md#ttl-inspection--refresh)).
**Google Cloud Storage** supports neither: an object cannot be edited in place
(see [Google Cloud Storage](gcs.md#limitations)).
**S3** supports neither, for the same reason
(see [S3](s3.md#limitations)).

**Cross-process whole-function invalidation** (a server-side key registry, via the
`KeyTrackableBackend` protocol): supported only by the tenant-scoped **Redis** backend that env
Expand Down Expand Up @@ -142,6 +145,12 @@ calling process knows (see [Whole-Function Invalidation](../features/l1-invalida
- The cached call is expensive and its result is worth keeping for hours or days
- A round trip to GCS on each L1 miss is acceptable ([not measured here](../performance.md#not-measured-here))

**Use [S3Backend](s3.md) when**:
- The workload runs on Lambda, ECS tasks or batch jobs, with no Redis and no disk that survives a run
- Your bucket is in an S3-compatible store rather than AWS S3: point `endpoint_url` at it ([S3-Compatible Stores](s3.md#s3-compatible-stores))
- The cached call is expensive and its result is worth keeping for hours or days
- A round trip to S3 on each L1 miss is acceptable ([not measured here](../performance.md#not-measured-here))

**Use [CacheKit Cloud](cachekitio.md) (`CachekitIOBackend`) when** *(closed beta: sign-up is by waitlist at [dash.cachekit.io](https://dash.cachekit.io))*:
- You want managed, zero-ops distributed caching
- Multi-region caching without operating Redis
Expand Down Expand Up @@ -228,7 +237,7 @@ it builds its own `CachekitIOBackend` and never consults `set_default_backend()`

If no explicit backend and no module-level default, `DefaultBackendProvider`
picks a backend at the function's first call from the one environment selector that is
set. The five prefixed selectors are mutually exclusive, with no precedence between them:
set. The six prefixed selectors are mutually exclusive, with no precedence between them:
set exactly one.

| Environment variable | Backend |
Expand All @@ -238,6 +247,7 @@ set exactly one.
| `CACHEKIT_MEMCACHED_SERVERS`| `MemcachedBackend` |
| `CACHEKIT_FILE_CACHE_DIR` | `FileBackend` |
| `CACHEKIT_GCS_BUCKET` | `GCSBackend` |
| `CACHEKIT_S3_BUCKET` | `S3Backend` |
| none of the above: `REDIS_URL` | Redis, as above |
| nothing set at all | plaintext, non-interop decorators: [L1-only](#4-nothing-configured-l1-only), no backend; secure and `encryption=True` decorators: Redis at `redis://localhost:6379`, tenant-scoped as above; `interop=` decorators: the same Redis, which interop refuses (see below) |

Expand All @@ -246,7 +256,7 @@ set exactly one.
`backend=RedisBackend()` instead ([Interop Mode](../features/interop-mode.md)). A
`CachekitIOBackend` from `CACHEKIT_API_KEY` prefixes nothing and serves interop as resolved.

Setting more than one of the five `CACHEKIT_*` selectors is ambiguous and raises
Setting more than one of the six `CACHEKIT_*` selectors is ambiguous and raises
`ConfigurationError` at first call. The decorator catches it, logs a WARNING on
the `cachekit.decorators.orchestrator` logger, and runs the function uncached:

Expand Down Expand Up @@ -425,6 +435,7 @@ first call (and runs uncached if nothing listens there): give it `backend=RedisB
| **DynamoDB** | One DynamoDB API request per operation ([not measured here](../performance.md#not-measured-here)) | Serverless, low-traffic | High availability |
| **Memcached** | One network round trip per operation ([not measured here](../performance.md#not-measured-here)) | Alternative to Redis | No persistence |
| **Google Cloud Storage** | One HTTPS request to GCS per ordinary operation ([request counts](gcs.md#requests-per-operation); [not measured here](../performance.md#not-measured-here)) | Expensive results kept across runs, no server | No locking; expiry checked on read |
| **S3** | One HTTPS request to S3 per operation ([request counts](s3.md#requests-per-operation); [not measured here](../performance.md#not-measured-here)) | Expensive results kept across runs, no server; S3-compatible stores | No locking; expiry checked on read; only the lifecycle rule removes expired entries |

---

Expand All @@ -434,6 +445,7 @@ first call (and runs uncached if nothing listens there): give it `backend=RedisB
- [File Backend](file.md) — Local disk, single-process, no infrastructure
- [Memcached Backend](memcached.md) — High-throughput, volatile, multi-process
- [Google Cloud Storage Backend](gcs.md) — Durable, one object per entry, no server
- [S3 Backend](s3.md) — Durable, one object per entry, no server; Amazon S3 and S3-compatible stores
- [CacheKit Cloud Backend](cachekitio.md) — Managed, zero-ops, zero-knowledge
- [Custom Backends](custom.md) — HTTP, DynamoDB, and your own implementations

Expand Down
1 change: 1 addition & 0 deletions docs/backends/gcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ STORAGE_EMULATOR_HOST=http://localhost:4443 uv run pytest tests/integration/test

- [Backend Guide](README.md) — Backend comparison and resolution priority
- [File Backend](file.md) — The same entry format on local disk
- [S3 Backend](s3.md) — The same entry format in an S3 bucket
- [Redis Backend](redis.md) — Low-latency shared caching with locking support
- [Configuration Guide](../configuration.md) — Full environment variable reference

Expand Down
Loading
Loading