| 57 | 63 | */ |
| 58 | 64 | async #removeCacheEntries() { |
| 59 | 65 | try { |
| 60 | 66 | if (!useDiskCache || this.removalQueue.sizelength === 0) { |
| 61 | 67 | return; |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | | - const keys = await this.instance().then(i => i.keys()); |
| 70 | + /** @type {Map<string, number>} */ |
| 65 | | - for (const item of this.removalQueue) { |
| 71 | + const latestTimestamps = new Map(); |
| 66 | | - const key = keys.find(k => k.startsWith(item)); |
| 72 | + for (const { item, timestamp } of this.removalQueue) { |
| 67 | | - if (key) { |
| 73 | + if (!latestTimestamps.has(item) || timestamp > (latestTimestamps.get(item) ?? 0)) { |
| 68 | | - await this.instance().then(i => i.removeItem(key)); |
| 74 | + latestTimestamps.set(item, timestamp); |
| 75 | + } |
| 76 | + } |
| 77 | + this.removalQueue.length = 0; |
| 78 | + |
| 79 | + const cache = await this.instance(); |
| 80 | + const keys = await cache.keys(); |
| 81 | + |
| 82 | + for (const [item, timestamp] of latestTimestamps.entries()) { |
| 83 | + const itemKeys = keys.filter(k => k.startsWith(item)); |
| 84 | + if (!itemKeys.length) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + for (const key of itemKeys) { |
| 88 | + const datumPath = cache.getDatumPath(key); |
| 89 | + if (!fs.existsSync(datumPath)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + const stat = fs.statSync(datumPath); |
| 93 | + if (stat.mtimeMs > timestamp) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + await cache.removeItem(key); |
| 69 | 97 | } |
| 70 | 98 | } |
| 71 | | - this.removalQueue.clear(); |
| 72 | 99 | } catch (error) { |
| 73 | 100 | console.error('Error while removing cache entries:', error); |
| 74 | 101 | } |
| 75 | 102 | }; |
| 76 | 103 | |
| 77 | 104 | /** |
| 78 | 105 | * Gets the disk cache instance. |