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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ 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.
One object per entry, in the file backend's format, in Amazon S3 or an S3-compatible store (`endpoint_url`). Cloudflare R2 is a tested one: see [Cloudflare R2](docs/backends/s3.md#cloudflare-r2). See [S3 Backend](docs/backends/s3.md) for IAM and the bucket lifecycle rule.

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

Expand Down
2 changes: 1 addition & 1 deletion docs/backends/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ calling process knows (see [Whole-Function Invalidation](../features/l1-invalida

**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))
- 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); [Cloudflare R2](s3.md#cloudflare-r2) is tested)
- 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))

Expand Down
37 changes: 36 additions & 1 deletion docs/backends/s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,46 @@ config = S3BackendConfig(

Credentials come from the same chain, so the store's access key goes in `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. With an `endpoint_url`, requests name the bucket in the path (`https://s3.example.com/my-cache-bucket/<object>`).

The backend uses four S3 operations: `GetObject` (whole, and with a `Range` header), `PutObject`, `DeleteObject` and `ListObjectsV2`. This repository's tests run it against the [moto](https://github.com/getmoto/moto) emulator; no other store is tested here, so check yours with `health_check()` and a round trip before relying on it.
The backend uses four S3 operations: `GetObject` (whole, and with a `Range` header), `PutObject`, `DeleteObject` and `ListObjectsV2`. This repository's tests run it against the [moto](https://github.com/getmoto/moto) emulator, and it has been run by hand against [Cloudflare R2](#cloudflare-r2). No other store is tested here, so check yours with `health_check()` and a round trip before relying on it.

> [!NOTE]
> boto3 adds a CRC32 checksum to each upload and asks for one on each download. Over HTTPS the upload carries it as a trailer after an `aws-chunked` body. A store that refuses either answers with an error on `set`; `AWS_REQUEST_CHECKSUM_CALCULATION=when_required` and `AWS_RESPONSE_CHECKSUM_VALIDATION=when_required` turn them off.

### Cloudflare R2

[Cloudflare R2](https://developers.cloudflare.com/r2/) works with the backend as it ships, with no checksum or other client setting to change. Set `endpoint_url` to your account's S3 API endpoint and `region` to `auto`:

```python
from cachekit.backends.s3 import S3BackendConfig

config = S3BackendConfig(
bucket="my-cache-bucket",
prefix="cachekit/",
endpoint_url="https://<ACCOUNT_ID>.r2.cloudflarestorage.com",
Comment thread
27Bslash6 marked this conversation as resolved.
Resolved
region="auto",
)
```

```bash
export CACHEKIT_S3_BUCKET="my-cache-bucket"
Comment thread
27Bslash6 marked this conversation as resolved.
Resolved
export CACHEKIT_S3_ENDPOINT_URL="https://<ACCOUNT_ID>.r2.cloudflarestorage.com"
export CACHEKIT_S3_REGION="auto"
export AWS_ACCESS_KEY_ID="<the R2 API token's Access Key ID>"
export AWS_SECRET_ACCESS_KEY="<the R2 API token's Secret Access Key>"
```

**Credentials.** Create an [R2 API token](https://developers.cloudflare.com/r2/api/tokens/) with the **Object Read & Write** permission, applied to the cache bucket only. Its Access Key ID and Secret Access Key are the two `AWS_` variables above. That one permission covers all four operations the backend uses, the `ListObjectsV2` of `health_check()` included, so the [IAM](#iam) policy above has no R2 counterpart. With an **Object Read only** token, reads and `health_check()` succeed and every `set` and `delete` raises an AUTHENTICATION `BackendError`: `health_check()` does not catch that mistake, a round trip does.

**Expired entries.** As on S3, the backend never deletes on a read, so give the bucket an [object lifecycle rule](https://developers.cloudflare.com/r2/buckets/object-lifecycles/) on the backend's `prefix`, with more days than your longest TTL. The rule is a bucket setting: add it in the dashboard or with Wrangler, since an Object Read & Write token cannot manage it.

```bash
npx wrangler r2 bucket lifecycle add my-cache-bucket expire-cachekit-entries cachekit/ --expire-days 8
```

R2 removes an object within about a day of the rule's expiry, and the warnings under [Expiry and the Lifecycle Rule](#expiry-and-the-lifecycle-rule) about entries older than the rule apply unchanged.

**What was checked.** One run against a real R2 bucket with boto3 1.44.18, on its default settings (2026-12-03): `set` then `get` of 1 KiB and 10 MiB values, `exists`, expiry of a 2-second TTL, `delete`, `health_check()` and a `@cache` round trip. R2 accepted boto3's default upload checksum, answered a missing object with 404 `NoSuchKey` (a miss) and a wrong secret key with 403 `SignatureDoesNotMatch` (an AUTHENTICATION `BackendError`). This repository's CI does not run against R2.

## Object Names and the Prefix

An entry's object key is the prefix followed by 32 hex characters, the BLAKE2b-128 hash of the cache key:
Expand Down
4 changes: 3 additions & 1 deletion llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ from cachekit.backends.s3 import S3Backend, S3BackendConfig

# Environment: CACHEKIT_S3_BUCKET=my-cache-bucket (auth: boto3's default credential chain)
# An S3-compatible store: add endpoint_url="https://s3.example.com" (CACHEKIT_S3_ENDPOINT_URL)
# Cloudflare R2 (tested): endpoint_url="https://<ACCOUNT_ID>.r2.cloudflarestorage.com", region="auto",
# and an R2 API token's keys in AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
config = S3BackendConfig(bucket="my-cache-bucket", prefix="cachekit/")
backend = S3Backend(config)

Expand Down Expand Up @@ -202,7 +204,7 @@ CACHEKIT_MASTER_KEY=hex-encoded-32-byte-key
- [File](docs/backends/file.md): Local filesystem caching
- [Memcached](docs/backends/memcached.md): High-throughput caching
- [Google Cloud Storage](docs/backends/gcs.md): One object per entry, file-backend format, IAM and lifecycle rule
- [S3](docs/backends/s3.md): One object per entry, file-backend format, IAM and lifecycle rule; Amazon S3 and S3-compatible stores
- [S3](docs/backends/s3.md): One object per entry, file-backend format, IAM and lifecycle rule; Amazon S3 and S3-compatible stores (Cloudflare R2 tested)
- [CacheKit Cloud](docs/backends/cachekitio.md): Managed backend (closed beta)
- [Custom Backends](docs/backends/custom.md): Implement BaseBackend

Expand Down
Loading