Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add upstash kv adapter #977

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-oranges-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Add upstash kv adapter.
40 changes: 40 additions & 0 deletions core/lib/kv/adapters/upstash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Redis } from '@upstash/redis';

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

import { MemoryKvAdapter } from './memory';

const memoryKv = new MemoryKvAdapter();

export class UpstashKvAdapter implements KvAdapter {
private upstashKv = Redis.fromEnv();
private memoryKv = memoryKv;

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

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

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

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

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

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

if (response === 'OK') {
return null;
}

return response;
}
}
6 changes: 6 additions & 0 deletions core/lib/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ async function createKVAdapter() {
return new VercelKvAdapter();
}

if (process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN) {
const { UpstashKvAdapter } = await import('./adapters/upstash');

return new UpstashKvAdapter();
}

const { MemoryKvAdapter } = await import('./adapters/memory');

return new MemoryKvAdapter();
Expand Down
1 change: 1 addition & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-tabs": "^1.0.4",
"@upstash/redis": "^1.31.3",
"@vercel/analytics": "^1.3.1",
"@vercel/kv": "^2.0.0",
"@vercel/speed-insights": "^1.0.11",
Expand Down
Loading
Loading