| 34 | } |
| 35 | |
| 36 | async cached(key: string, fresh: () => Promise<string>): Promise<string> { |
| 37 | this.usedKeys.add(this.cacheKey(key)); |
| 38 | try { |
| 39 | const text = await fresh(); |
| 40 | await this.write(key, text); |
| 41 | return text; |
| 42 | } catch (error) { |
| 43 | const cachedEntry = await this.read(key); |
| 44 | if (cachedEntry !== null) { |
| 45 | this.warnings.push({ |
| 46 | url: key, |
| 47 | reason: error instanceof Error ? error.message : String(error), |
| 48 | cachedAt: cachedEntry.cachedAt |
| 49 | }); |
| 50 | if (cachedEntry.cachedAt) { |
| 51 | const ageDays = (Date.now() - new Date(cachedEntry.cachedAt).getTime()) / 86_400_000; |
| 52 | if (ageDays > STALE_CACHE_DAYS) { |
| 53 | this.staleWarnings.push({ url: key, ageDays: Math.floor(ageDays) }); |
| 54 | } |
| 55 | } |
| 56 | return cachedEntry.text; |
| 57 | } |
| 58 | throw new Error( |
| 59 | `Failed to fetch ${key} and no cache available: ${ |
| 60 | error instanceof Error ? error.message : String(error) |
| 61 | }` |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | async pruneOrphans(): Promise<string[]> { |
| 67 | const removed: string[] = []; |