You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-13Lines changed: 48 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
# Cache Tag
2
2
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
5
5
[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.).
6
6
7
7
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
18
18
This application is broken up into three [Workers](https://developers.cloudflare.com/workers/), three
19
19
[Queues](https://developers.cloudflare.com/queues/), and one [D1](https://developers.cloudflare.com/d1/) database.
20
20
21
-
### Workers
22
-
23
-
#### [Watcher](./workers/watcher/)
21
+
### [Watcher](./workers/watcher/)
24
22
25
23
This worker watches requests to the Cloudflare [Cache](https://developers.cloudflare.com/cache/) / Origin, captures the
26
24
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
31
29
> custom `X-Cache-Tag` header instead.
32
30
33
31
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
35
34
to the [zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) in which the request is
36
35
made too. For example, a purge request to `https://example.com/.cloudflare/purge` would only purge resources from the
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
58
59
[worker](https://developers.cloudflare.com/workers/) is collocated in the
59
60
[same data center](https://developers.cloudflare.com/workers/configuration/smart-placement/) as
60
61
[Watcher](./workers/watcher/).
61
62
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
64
65
[zone](https://developers.cloudflare.com/fundamentals/setup/accounts-and-zones/#zones) information is provided
65
66
(via the [CF-Worker](https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-worker) header),
66
67
matching resources from **all** zones will be purged.
67
68
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.
0 commit comments