feat: better cache some stuff
This commit is contained in:
24
lib/cache/cache.ts
vendored
24
lib/cache/cache.ts
vendored
@@ -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>>;
|
||||
};
|
||||
|
Reference in New Issue
Block a user