Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMGEvans committed Aug 29, 2023
1 parent 28e817e commit b744c5b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/autoCacheBust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export function isOlderThan(date: Date, hours: number | string): boolean {
return diffInHours >= Number(hours);
}

export const RECORDS_BATCH_SIZE = 500;
const RECORDS_BATCH_SIZE = 500;

function R2cacheDeletion() {
/**
* Creates a cache object with two methods: add and getKeys.
* */
function r2CacheCollector() {
let keys: string[] = [];
return {
add: function (key: string) {
Expand All @@ -30,21 +33,21 @@ function R2cacheDeletion() {
};
}

async function deleteKeys(env: Env, cacheDeletion: ReturnType<typeof R2cacheDeletion>) {
async function deleteKeys(env: Env, cacheDeletion: ReturnType<typeof r2CacheCollector>) {
if (cacheDeletion.getKeys().length > 0) {
await env.R2_STORE.delete(cacheDeletion.getKeys());
}
}

async function processList(list: R2Objects, env: Env) {
const cacheDeletion = R2cacheDeletion();
const r2CacheToDelete = r2CacheCollector();
for (const object of list.objects) {
if (isOlderThan(object.uploaded, env.EXPIRATION_HOURS)) {
cacheDeletion.add(object.key);
r2CacheToDelete.add(object.key);
}
}

await deleteKeys(env, cacheDeletion);
await deleteKeys(env, r2CacheToDelete);
}

export async function bustOldCache(env: Env, cursor?: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HTTPException } from 'hono/http-exception';
import { bearerAuth } from 'hono/bearer-auth';
import { cors } from 'hono/cors';
import { z } from 'zod';
import { deleteOldCache } from './autoCacheBust';
import { bustOldCache } from './autoCacheBust';

export const router = new Hono<{ Bindings: Env }>();

Expand All @@ -27,7 +27,7 @@ router.use('*', async (c, next) => {

router.post('/artifacts/manual-cache-bust', zValidator('json', z.object({ expireInHours: z.number().optional() })), async (c) => {
const { expireInHours } = c.req.valid('json');
await deleteOldCache({
await bustOldCache({
...c.env,
EXPIRATION_HOURS: expireInHours ?? c.env.EXPIRATION_HOURS,
});
Expand Down
2 changes: 1 addition & 1 deletion wrangler.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compatibility_date": "2023-08-14",
"name": "r2-archive",
"vars": {
"EXPIRATION_HOURS": 168
"EXPIRATION_HOURS": 144
},

"r2_buckets": [
Expand Down

0 comments on commit b744c5b

Please sign in to comment.