Skip to content

Commit

Permalink
simplified arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMGEvans committed Sep 1, 2023
1 parent ed26ce2 commit b893379
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Overview

This is a Worker that will act as an event server for caching TurboRepo artifacts. Compliant with the TurboRepo API, for remote caching, it will store the cache artifacts in a Cloudflare R2 bucket and purge the cache on a schedule. This allows for all the benefits of remote caching TurboRepo artifacts on Cloudflare's edge network.
This is a Worker that will act as an event server for caching TurboRepo artifacts. Compliant with the TurboRepo API, for remote caching, it will store the cache artifacts in a Cloudflare R2 bucket and purge the R2 Objects on a schedule, using [R2 Object lifecycle rules](https://blog.cloudflare.com/introducing-object-lifecycle-management-for-cloudflare-r2/). This allows for all the benefits of remote caching TurboRepo artifacts on Cloudflare's edge network.

This will require a Cloudflare account, with a zone and R2.

Expand Down
16 changes: 8 additions & 8 deletions src/autoCacheBust.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// For scheduled R2 cache busting, the R2 lifecycle rules can be used to delete an object after a certain amount of time.

async function bustEntireCache(list: R2Objects, env: Env) {
for (const object of list.objects) {
await env.R2_ARTIFACT_ARCHIVE.delete(object.key);
}
async function bustEntireCache(list: R2Objects, R2_ARTIFACT_ARCHIVE: Env['R2_ARTIFACT_ARCHIVE']) {
const allObjectKeys = list.objects.map((o) => o.key);

await R2_ARTIFACT_ARCHIVE.delete(allObjectKeys);
}

export async function bustOldCache(env: Env, cursor?: string) {
const list = await env.R2_ARTIFACT_ARCHIVE.list({
export async function bustOldCache(R2_ARTIFACT_ARCHIVE: Env['R2_ARTIFACT_ARCHIVE'], cursor?: string) {
const list = await R2_ARTIFACT_ARCHIVE.list({
limit: 500,
cursor,
});
await bustEntireCache(list, env);
await bustEntireCache(list, R2_ARTIFACT_ARCHIVE);

if (list.truncated) {
await bustOldCache(env, list.cursor);
await bustOldCache(R2_ARTIFACT_ARCHIVE, list.cursor);
}
}
4 changes: 1 addition & 3 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ router.post('/artifacts/manual-cache-bust', async (c) => {
/**
* manual cache busting, it will bust the entire cache.
*/
await bustOldCache({
...c.env,
});
await bustOldCache(c.env.R2_ARTIFACT_ARCHIVE);

// maybe this could return the keys that were busted?
return c.json({ success: true });
Expand Down

0 comments on commit b893379

Please sign in to comment.