Skip to content

Commit 3faf920

Browse files
committed
Update docs
1 parent 7eed0d2 commit 3faf920

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Cache Tag
22

3-
[Cloudflare](https://www.cloudflare.com/) has the ability to index cached resources by _tag_ which allows those resources be
4-
[purged by tag](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/). However, this feature is
3+
[Cloudflare](https://www.cloudflare.com/) has the ability to index cached resources by _tag_ which allows those resources
4+
be [purged by tag](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/). However, this feature is
55
[only available for Enterprise customers](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#:~:text=Note%20that%20Tag%2C%20Hostname%20and%20Prefix%20purges%20are%20only%20available%20for%20Enterprise%20customers.).
66

77
Despite this limitation, an index can be built using [Workers](https://developers.cloudflare.com/workers/),
@@ -18,9 +18,7 @@ Despite this limitation, an index can be built using [Workers](https://developer
1818
This application is broken up into three [Workers](https://developers.cloudflare.com/workers/), three
1919
[Queues](https://developers.cloudflare.com/queues/), and one [D1](https://developers.cloudflare.com/d1/) database.
2020

21-
### Workers
22-
23-
#### [Watcher](./workers/watcher/)
21+
### [Watcher](./workers/watcher/)
2422

2523
This worker watches requests to the Cloudflare [Cache](https://developers.cloudflare.com/cache/) / Origin, captures the
2624
tags, and sends them to the [Controller](./workers/controller/) in order to be persisted.
@@ -31,12 +29,13 @@ tags, and sends them to the [Controller](./workers/controller/) in order to be p
3129
> custom `X-Cache-Tag` header instead.
3230
3331
The worker also exposes a `/.cloudflare/purge` endpoint that allows tags to be purged. This endpoint matches the
34-
[interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix), but only allows `tags`. The tags that are purged will be scoped
32+
[interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix),
33+
but only allows `tags`. The tags that are purged will be scoped
3534
to the [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) in which the request is
3635
made too. For example, a purge request to `https://example.com/.cloudflare/purge` would only purge resources from the
3736
`example.com` [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones).
3837

39-
#### [Controller](./workers/controller/)
38+
### [Controller](./workers/controller/)
4039

4140
A [Worker](https://developers.cloudflare.com/workers/) is an
4241
[account-level resource](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#accounts), but
@@ -53,18 +52,54 @@ not exist when using
5352
retrieve the header is by making a request to the [worker](https://developers.cloudflare.com/workers/) on the provided
5453
[workers.dev subdomain](https://developers.cloudflare.com/workers/configuration/routing/workers-dev/).
5554

56-
The [Controller](./workers/controller/) exists primarily as an intermediary between [Watcher](./workers/watcher/) and [Handler](./workers/handler/) to collect [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones)
57-
information. It is **not** included as a part of [Handler](./workers/handler/) in order to ensure that the
55+
The [Controller](./workers/controller/) exists primarily as an intermediary between [Watcher](./workers/watcher/) and
56+
[Handler](./workers/handler/) to collect
57+
[zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) information. It is **not**
58+
included as a part of [Handler](./workers/handler/) in order to ensure that the
5859
[worker](https://developers.cloudflare.com/workers/) is collocated in the
5960
[same data center](https://developers.cloudflare.com/workers/configuration/smart-placement/) as
6061
[Watcher](./workers/watcher/).
6162

62-
The [worker](https://developers.cloudflare.com/workers/) also exposes a `/purge` endpoint that allows tags to be purged. This endpoint matches the
63-
[interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix), but only allows `tags`. If no
63+
The [worker](https://developers.cloudflare.com/workers/) also exposes a `/purge` endpoint that allows tags to be purged.
64+
This endpoint matches the [interface of the Cloudflare endpoint](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-tag-host-or-prefix), but only allows `tags`. If no
6465
[zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) information is provided
6566
(via the [CF-Worker](https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-worker) header),
6667
matching resources from **all** zones will be purged.
6768

68-
#### [Handler](./workers/handler/)
69+
After receiving and validating requests to either the `/capture` or `/purge` endpoints, the
70+
[worker](https://developers.cloudflare.com/workers/) adds the requests to the `cache-capture` and `cache-purge-tag`
71+
queues respectively.
72+
73+
### [Handler](./workers/handler/)
74+
75+
This [worker](https://developers.cloudflare.com/workers/) listens to all three queues and handles them.
76+
77+
When a message is received from [Controller](./workers/controller/) in the `cache-capture` queue; the URL, zone, and
78+
tags are stored in the [D1](https://developers.cloudflare.com/d1/) database.
79+
80+
A message received from [Controller](./workers/controller/) in the `cache-purge-tag` queue results in the URLs being
81+
looked up in the [D1](https://developers.cloudflare.com/d1/) database from the provided tag, and re-queing those URLs by
82+
adding each one to the `cache-purge-url` queue. Since this will result in the resource being eventually removed from the
83+
cache, the URL and all tags associated with it are removed from the [D1](https://developers.cloudflare.com/d1/) database.
84+
85+
Finally, when a message is received from the `cache-purge-url` queue, the URLs are
86+
[purged with Cloudflare's API](https://developers.cloudflare.com/api/operations/zone-purge#purge-cached-content-by-url).
87+
88+
## Usage
89+
90+
I am not aware of a good way to distribute this application for use on your own other than forking it and modifying it.
91+
It is [licensed under the AGPL-3.0 license](./LICENSE.md) so you are free to modify it under the temrs of that license.
92+
I thought about using Terraform in order to make it easier for others to deploy on their own, but it seemed like
93+
overkill for my purposes. I'm happy to accept PRs that make life easier.
94+
95+
## Authentication
96+
97+
I chose to use the `API_TOKEN` [secret](https://developers.cloudflare.com/workers/configuration/secrets/) for
98+
authentication/authorization to the [Controller](./workers/controller/) and to use the same token to make requests to
99+
the Cloudflare API. This simplified the approach by only having to have a single secret in the worker and sharing that
100+
secret with the Origin server. This allows the origin to make requests to the Cloudflare API or the Worker seemlessly.
101+
102+
The minimum permissions needed are:
69103

70-
_@todo_
104+
- [Zone Read](https://developers.cloudflare.com/fundamentals/api/reference/permissions/#:~:text=zone%20level%20settings.-,Zone%20Read,-Grants%20read%20access)
105+
- [Cache Purge](https://developers.cloudflare.com/fundamentals/api/reference/permissions/#:~:text=Management%20feedback.-,Cache%20Purge,-Grants%20access%20to)

0 commit comments

Comments
 (0)