Add in-memory caching for online count, enable compression, and add staleTimes for router cache
This commit is contained in:
1 parent
b058a3827b
commit
deac10e00a
5 files changed
+51
-10
No files matched your search
@@ -0,0 +1,17 @@
|
||||
type CacheEntry<T> = { data: T; expiresAt: number };
|
||||
const store = new Map<string, CacheEntry<unknown>>();
|
||||
|
||||
export function cached<T>(key: string, ttlMs: number, fn: () => Promise<T>): Promise<T> {
|
||||
const existing = store.get(key);
|
||||
if (existing && existing.expiresAt > Date.now()) {
|
||||
return Promise.resolve(existing.data as T);
|
||||
}
|
||||
return fn().then((data) => {
|
||||
store.set(key, { data, expiresAt: Date.now() + ttlMs });
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export function bustCache(key: string): void {
|
||||
store.delete(key);
|
||||
}
|
||||
Reference in new issue
Block a user