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

CacheStorage #260

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
49 changes: 49 additions & 0 deletions src/server/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export interface Cache {
get(key: string, fallback?: unknown): Promise<unknown | null>;
set(key: string, value: unknown, ttl?: number): Promise<void>;
delete(key: string): Promise<void>;
has(key: string): Promise<boolean>;
clear(): Promise<void>;
}

export function createCacheStorage(implementation: Cache) {
return implementation;
}

export function createMemoryCacheStorage() {
let cache = new Map<string, { ttl?: number; data: unknown }>();

return {
async get(key: string, fallback: unknown = null) {
const item = cache.get(key);
if (!item) return fallback;
if (item.ttl && item.ttl < Date.now()) {
cache.delete(key);
return fallback;
}
return item.data;
},

async set(key: string, value: unknown, ttl?: number) {
cache.set(key, { ttl: ttl ? Date.now() + ttl : undefined, data: value });
},

async delete(key: string) {
cache.delete(key);
},

async has(key: string) {
if (!cache.has(key)) return false;
const item = cache.get(key);
if (item && item.ttl && item.ttl < Date.now()) {
cache.delete(key);
return false;
}
return true;
},

async clear() {
cache.clear();
},
};
}
62 changes: 62 additions & 0 deletions test/server/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createMemoryCacheStorage } from "../../src";

describe(createMemoryCacheStorage.name, () => {
test("get returns null for missing key", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.get("key")).resolves.toBeNull();
});

test("get returns the fallback for missing key", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.get("key", "fallback")).resolves.toBe("fallback");
});

test("set saves a value", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key", "value")).resolves.toBeUndefined();
await expect(cache.get("key")).resolves.toBe("value");
});

test("set saves a value with a ttl", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key", "value", 10)).resolves.toBeUndefined();
await expect(cache.get("key")).resolves.toBe("value");
await new Promise((resolve) => setTimeout(resolve, 11));
await expect(cache.get("key")).resolves.toBeNull();
});

test("delete removes a value", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key", "value")).resolves.toBeUndefined();
await expect(cache.get("key")).resolves.toBe("value");
await expect(cache.delete("key")).resolves.toBeUndefined();
await expect(cache.get("key")).resolves.toBeNull();
});

test("has returns false for missing key", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.has("key")).resolves.toBe(false);
});

test("has returns false for expired key", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key", "value", 10)).resolves.toBeUndefined();
await new Promise((resolve) => setTimeout(resolve, 11));
await expect(cache.has("key")).resolves.toBe(false);
});

test("has returns true for existing key", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key", "value")).resolves.toBeUndefined();
await expect(cache.has("key")).resolves.toBe(true);
});

test("clear removes all values", async () => {
let cache = createMemoryCacheStorage();
await expect(cache.set("key-1", "value")).resolves.toBeUndefined();
await expect(cache.set("key-2", "value")).resolves.toBeUndefined();
await expect(cache.clear()).resolves.toBeUndefined();
await expect(cache.has("key-1")).resolves.toBe(false);
await expect(cache.has("key-2")).resolves.toBe(false);
});
});