(cacheName: string, path: string, cacheMb: number)
| 54 | private readonly cache: LRUCache<string, CacheEntry>; |
| 55 | |
| 56 | constructor(cacheName: string, path: string, cacheMb: number) { |
| 57 | super(cacheName, `OnDiskCache(${path}, ${cacheMb}mb)`, 'disk'); |
| 58 | this.path = path; |
| 59 | this.cacheMb = cacheMb; |
| 60 | this.cache = new LRUCache({ |
| 61 | maxSize: cacheMb * 1024 * 1024, |
| 62 | sizeCalculation: n => n.size, |
| 63 | noDisposeOnSet: true, |
| 64 | dispose: value => fs.unlink(value.path, () => {}), |
| 65 | }); |
| 66 | fs.mkdirSync(path, {recursive: true}); |
| 67 | const info = getAllFiles(path) |
| 68 | .map(({name, fullPath}) => { |
| 69 | const stat = fs.statSync(fullPath); |
| 70 | if (stat.size === 0 || fullPath.endsWith('.tmp')) { |
| 71 | logger.info(`Removing old temporary or broken empty file ${fullPath}`); |
| 72 | fs.unlink(fullPath, () => {}); |
| 73 | return undefined; |
| 74 | } |
| 75 | return { |
| 76 | key: name, |
| 77 | sort: stat.ctimeMs, |
| 78 | data: { |
| 79 | path: fullPath, |
| 80 | size: stat.size, |
| 81 | }, |
| 82 | }; |
| 83 | }) |
| 84 | .filter(Boolean); |
| 85 | |
| 86 | // Sort oldest first |
| 87 | |
| 88 | // @ts-expect-error filter(Boolean) should have sufficed but doesn't |
| 89 | info.sort((x, y) => x.sort - y.sort); |
| 90 | for (const i of info) { |
| 91 | // @ts-expect-error |
| 92 | this.cache.set(i.key, i.data); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | override statString(): string { |
| 97 | return ( |
nothing calls this directly
no test coverage detected