mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
init app resources
This commit is contained in:
45
app/src/adapter/cloudflare/drivers/cache.ts
Normal file
45
app/src/adapter/cloudflare/drivers/cache.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { ICacheDriver } from "core/drivers";
|
||||
|
||||
interface WorkersKVCacheOptions {
|
||||
// default time-to-live in seconds
|
||||
defaultTTL?: number;
|
||||
// prefix for the cache key
|
||||
cachePrefix?: string;
|
||||
}
|
||||
|
||||
export class WorkersKVCacheDriver implements ICacheDriver {
|
||||
protected readonly kv: KVNamespace;
|
||||
protected readonly defaultTTL?: number;
|
||||
protected readonly cachePrefix: string;
|
||||
|
||||
constructor(kv: KVNamespace, options: WorkersKVCacheOptions = {}) {
|
||||
this.kv = kv;
|
||||
this.cachePrefix = options.cachePrefix ?? "";
|
||||
this.defaultTTL = options.defaultTTL;
|
||||
}
|
||||
|
||||
protected getKey(key: string): string {
|
||||
return this.cachePrefix + key;
|
||||
}
|
||||
|
||||
async get(key: string): Promise<string | undefined> {
|
||||
const value = await this.kv.get(this.getKey(key));
|
||||
return value === null ? undefined : value;
|
||||
}
|
||||
|
||||
async set(key: string, value: string, ttl?: number): Promise<void> {
|
||||
let expirationTtl = ttl ?? this.defaultTTL;
|
||||
if (expirationTtl) {
|
||||
expirationTtl = Math.max(expirationTtl, 60);
|
||||
}
|
||||
await this.kv.put(this.getKey(key), value, { expirationTtl: expirationTtl });
|
||||
}
|
||||
|
||||
async del(key: string): Promise<void> {
|
||||
await this.kv.delete(this.getKey(key));
|
||||
}
|
||||
}
|
||||
|
||||
export const cacheWorkersKV = (kv: KVNamespace, options?: WorkersKVCacheOptions) => {
|
||||
return new WorkersKVCacheDriver(kv, options);
|
||||
};
|
||||
34
app/src/adapter/cloudflare/drivers/cache.vitest.ts
Normal file
34
app/src/adapter/cloudflare/drivers/cache.vitest.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { describe, vi, afterAll, beforeAll } from "vitest";
|
||||
import { cacheWorkersKV } from "./cache";
|
||||
import { viTestRunner } from "adapter/node/vitest";
|
||||
import { cacheDriverTestSuite } from "core/drivers/cache/cache-driver-test-suite";
|
||||
import { Miniflare } from "miniflare";
|
||||
|
||||
describe("cacheWorkersKV", async () => {
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
afterAll(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
const mf = new Miniflare({
|
||||
modules: true,
|
||||
script: "export default { async fetch() { return new Response(null); } }",
|
||||
kvNamespaces: ["KV"],
|
||||
});
|
||||
|
||||
const kv = (await mf.getKVNamespace("KV")) as unknown as KVNamespace;
|
||||
|
||||
cacheDriverTestSuite(viTestRunner, {
|
||||
makeCache: () => cacheWorkersKV(kv),
|
||||
setTime: (ms: number) => {
|
||||
vi.advanceTimersByTime(ms);
|
||||
},
|
||||
options: {
|
||||
minTTL: 60,
|
||||
// doesn't work with miniflare
|
||||
skipTTL: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user