diff --git a/.changeset/lemon-beans-help.md b/.changeset/lemon-beans-help.md new file mode 100644 index 0000000..d5590c2 --- /dev/null +++ b/.changeset/lemon-beans-help.md @@ -0,0 +1,5 @@ +--- +'turborepo-remote-cache-cf': minor +--- + +Use KV key expiry to cost effectively remove cache keys diff --git a/src/storage/kv-storage.ts b/src/storage/kv-storage.ts index 4791e25..387a5f0 100644 --- a/src/storage/kv-storage.ts +++ b/src/storage/kv-storage.ts @@ -9,9 +9,11 @@ import { export class KvStorage implements StorageInterface { private KV_STORE: KVNamespace; + private EXPIRATION_IN_SECONDS?: number; - constructor(kvNamespace: KVNamespace) { + constructor(kvNamespace: KVNamespace, expirationInHours?: number) { this.KV_STORE = kvNamespace; + this.EXPIRATION_IN_SECONDS = expirationInHours ? expirationInHours * 60 * 60 : undefined; } async listWithMetadata(options?: ListFilterOptions): Promise { @@ -56,6 +58,7 @@ export class KvStorage implements StorageInterface { createdAtEpochMilliseconds: Date.now(), customMetadata: metadata, }, + expirationTtl: this.EXPIRATION_IN_SECONDS, }); } diff --git a/src/storage/storage-manager.ts b/src/storage/storage-manager.ts index db81716..64ee747 100644 --- a/src/storage/storage-manager.ts +++ b/src/storage/storage-manager.ts @@ -14,7 +14,7 @@ export class StorageManager { this.r2Storage = new R2Storage(env.R2_STORE); } if (env.KV_STORE) { - this.kvStorage = new KvStorage(env.KV_STORE); + this.kvStorage = new KvStorage(env.KV_STORE, env.BUCKET_OBJECT_EXPIRATION_HOURS); } if (!this.r2Storage && !this.kvStorage) { throw new InvalidStorageError('No storage provided'); diff --git a/tests/routes/v8/artifacts.test.ts b/tests/routes/v8/artifacts.test.ts index cc53e6d..2648793 100644 --- a/tests/routes/v8/artifacts.test.ts +++ b/tests/routes/v8/artifacts.test.ts @@ -297,7 +297,6 @@ describe('v8 Artifacts API', () => { ]), }); const res = await app.fetch(request, workerEnv, ctx); - console.log(await res.text()); expect(res.status).toBe(200); }); }); diff --git a/tests/storage/kv-storage.test.ts b/tests/storage/kv-storage.test.ts index f9c2ab4..d5e5ae5 100644 --- a/tests/storage/kv-storage.test.ts +++ b/tests/storage/kv-storage.test.ts @@ -131,6 +131,14 @@ describe('kv-storage', () => { const bufferView = new Uint8Array(dataAsBuffer); expect(bufferView).toEqual(new Uint8Array(buffer)); }); + + test('sets expiration TTL when available', async () => { + storage = new KvStorage(workerEnv.KV_STORE, 1); + await storage.write('key1', 'value1'); + const result = await storage.read('key1'); + const dataAsText = await StorageManager.readableStreamToText(result!); + expect(dataAsText).toBe('value1'); + }); }); describe('delete()', () => {