Function
cacheWithTTL
(
ttlMs: number,
fn: () => Promise<T>,
)
Source from the content-addressed store, hash-verified
| 11 | * @returns {() => Promise<T | undefined>} |
| 12 | */ |
| 13 | export function cacheWithTTL<T>( |
| 14 | ttlMs: number, |
| 15 | fn: () => Promise<T>, |
| 16 | ): () => Promise<T | undefined> { |
| 17 | let lastFetchTime = 0; |
| 18 | let cache: T | undefined; |
| 19 | |
| 20 | return async () => { |
| 21 | if (lastFetchTime < Date.now() - ttlMs) { |
| 22 | lastFetchTime = Date.now(); |
| 23 | cache = await fn(); |
| 24 | } |
| 25 | return cache; |
| 26 | }; |
| 27 | } |
Tested by
no test coverage detected