Skip to content

Commit

Permalink
feat(kv): Set KV key expiration to improve cost management (#449)
Browse files Browse the repository at this point in the history
* feat(kv): Set KV key expiration to improve cost management

* Add changeset

* fix lint
  • Loading branch information
AdiRishi authored May 11, 2024
1 parent 5bb958b commit b765ed4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-beans-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'turborepo-remote-cache-cf': minor
---

Use KV key expiry to cost effectively remove cache keys
5 changes: 4 additions & 1 deletion src/storage/kv-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListResultWithMetadata> {
Expand Down Expand Up @@ -56,6 +58,7 @@ export class KvStorage implements StorageInterface {
createdAtEpochMilliseconds: Date.now(),
customMetadata: metadata,
},
expirationTtl: this.EXPIRATION_IN_SECONDS,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/storage/storage-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion tests/routes/v8/artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
8 changes: 8 additions & 0 deletions tests/storage/kv-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit b765ed4

Please sign in to comment.