feat: better cache some stuff

This commit is contained in:
2023-08-09 23:51:40 +02:00
parent 6587ee689b
commit 3232f14bf7
10 changed files with 153 additions and 177 deletions

24
lib/cache/cache.ts vendored
View File

@@ -104,3 +104,27 @@ export async function set<T extends RedisValue>(
}
return res;
}
export const cacheFunction = async <T extends (() => Promise<unknown>)>(
{
fn,
id,
options = {},
}: {
fn: T;
id: string;
options?: RedisOptions;
},
): Promise<Awaited<ReturnType<T>>> => {
const cacheResult = await get(id) as string;
if (cacheResult) {
return JSON.parse(cacheResult) as Awaited<ReturnType<typeof fn>>;
}
const result = await fn();
set(id, JSON.stringify(result), options);
return result as Awaited<ReturnType<typeof fn>>;
};