mirror of
https://github.com/docmost/docmost.git
synced 2026-08-29 18:14:58 +08:00
28 lines
620 B
TypeScript
28 lines
620 B
TypeScript
import { Cache } from 'cache-manager';
|
|
|
|
export async function withCache<T>(
|
|
cacheManager: Cache,
|
|
key: string,
|
|
ttlMs: number,
|
|
fn: () => Promise<T>,
|
|
): Promise<T> {
|
|
try {
|
|
const cached = await cacheManager.get<{ v: T }>(key);
|
|
if (cached !== undefined && cached !== null) {
|
|
return cached.v;
|
|
}
|
|
} catch (err) {
|
|
console.warn(`[withCache] get failed for "${key}", falling back to source`, err);
|
|
}
|
|
|
|
const value = await fn();
|
|
|
|
try {
|
|
await cacheManager.set(key, { v: value }, ttlMs);
|
|
} catch (err) {
|
|
console.warn(`[withCache] set failed for "${key}"`, err);
|
|
}
|
|
|
|
return value;
|
|
}
|