From bbf34c177daba3f33a677f777e96d29753095760 Mon Sep 17 00:00:00 2001 From: Ray Walker Date: Thu, 3 Dec 2026 13:08:27 +1100 Subject: [PATCH] docs(backends): R2 with the S3 backend The S3 backend was run against a real Cloudflare R2 bucket and needed no change: boto3's default upload checksum is accepted, a missing object is a 404 and a wrong secret a 403. Document the endpoint, region, token permission and lifecycle rule so R2 users do not have to rediscover them. --- README.md | 2 +- docs/backends/README.md | 2 +- docs/backends/s3.md | 37 ++++++++++++++++++++++++++++++++++++- llms.txt | 4 +++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 531e029e..f3b778b9 100644 --- a/README.md +++ b/README.md @@ -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]`* diff --git a/docs/backends/README.md b/docs/backends/README.md index 9c093ffb..baf15c69 100644 --- a/docs/backends/README.md +++ b/docs/backends/README.md @@ -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)) diff --git a/docs/backends/s3.md b/docs/backends/s3.md index c4b7aaf0..7c555f3a 100644 --- a/docs/backends/s3.md +++ b/docs/backends/s3.md @@ -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/`). -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://.r2.cloudflarestorage.com", + region="auto", +) +``` + +```bash +export CACHEKIT_S3_BUCKET="my-cache-bucket" +export CACHEKIT_S3_ENDPOINT_URL="https://.r2.cloudflarestorage.com" +export CACHEKIT_S3_REGION="auto" +export AWS_ACCESS_KEY_ID="" +export AWS_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: diff --git a/llms.txt b/llms.txt index a1c1e281..4d953ca4 100644 --- a/llms.txt +++ b/llms.txt @@ -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://.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) @@ -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