Skip to content

Commit

Permalink
feat(core): vercel kv adapter cache (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
deini committed Apr 18, 2024
1 parent 85bd336 commit 86403a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-candles-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

best-effort in memory cache for vercel kv adapter
19 changes: 15 additions & 4 deletions apps/core/lib/kv/adapters/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ import { kv } from '@vercel/kv';

import { KvAdapter, SetCommandOptions } from '../types';

import { MemoryKvAdapter } from './memory';

const memoryKv = new MemoryKvAdapter();

export class VercelKvAdapter implements KvAdapter {
constructor(private adapter = kv) {}
private vercelKv = kv;
private memoryKv = memoryKv;

async get<Data>(key: string) {
return this.adapter.get<Data>(key);
const memoryValue = await this.memoryKv.get<Data>(key);

return memoryValue ?? this.vercelKv.get<Data>(key);
}

async mget<Data>(...keys: string[]) {
return this.adapter.mget<Data[]>(keys);
const memoryValues = await this.memoryKv.mget<Data>(...keys);

return memoryValues.length ? memoryValues : this.vercelKv.mget<Data[]>(keys);
}

async set<Data, Options extends SetCommandOptions = SetCommandOptions>(
key: string,
value: Data,
opts?: Options,
) {
const response = await this.adapter.set(key, value, opts);
await this.memoryKv.set(key, value, opts);

const response = await this.vercelKv.set(key, value, opts);

if (response === 'OK') {
return null;
Expand Down

0 comments on commit 86403a6

Please sign in to comment.