|
| 1 | +const { LRUCache } = require('lru-cache') |
| 2 | +const { getHeapStatistics } = require('node:v8') |
| 3 | +const { log } = require('proc-log') |
| 4 | + |
| 5 | +// This is an in-memory cache that Pacote uses for packuments. |
| 6 | +// Packuments are usually cached on disk. This allows for rapid re-requests |
| 7 | +// of the same packument to bypass disk reads. The tradeoff here is memory |
| 8 | +// usage for disk reads. |
| 9 | +class PackumentCache extends LRUCache { |
| 10 | + static #heapLimit = Math.floor(getHeapStatistics().heap_size_limit) |
| 11 | + |
| 12 | + #sizeKey |
| 13 | + #disposed = new Set() |
| 14 | + |
| 15 | + #log (...args) { |
| 16 | + log.silly('packumentCache', ...args) |
| 17 | + } |
| 18 | + |
| 19 | + constructor ({ |
| 20 | + // How much of this.#heapLimit to take up |
| 21 | + heapFactor = 0.25, |
| 22 | + // How much of this.#maxSize we allow any one packument to take up |
| 23 | + // Anything over this is not cached |
| 24 | + maxEntryFactor = 0.5, |
| 25 | + sizeKey = '_contentLength', |
| 26 | + } = {}) { |
| 27 | + const maxSize = Math.floor(PackumentCache.#heapLimit * heapFactor) |
| 28 | + const maxEntrySize = Math.floor(maxSize * maxEntryFactor) |
| 29 | + super({ |
| 30 | + maxSize, |
| 31 | + maxEntrySize, |
| 32 | + sizeCalculation: (p) => { |
| 33 | + // Don't cache if we dont know the size |
| 34 | + // Some versions of pacote set this to `0`, newer versions set it to `null` |
| 35 | + if (!p[sizeKey]) { |
| 36 | + return maxEntrySize + 1 |
| 37 | + } |
| 38 | + if (p[sizeKey] < 10_000) { |
| 39 | + return p[sizeKey] * 2 |
| 40 | + } |
| 41 | + if (p[sizeKey] < 1_000_000) { |
| 42 | + return Math.floor(p[sizeKey] * 1.5) |
| 43 | + } |
| 44 | + // It is less beneficial to store a small amount of super large things |
| 45 | + // at the cost of all other packuments. |
| 46 | + return maxEntrySize + 1 |
| 47 | + }, |
| 48 | + dispose: (v, k) => { |
| 49 | + this.#disposed.add(k) |
| 50 | + this.#log(k, 'dispose') |
| 51 | + }, |
| 52 | + }) |
| 53 | + this.#sizeKey = sizeKey |
| 54 | + this.#log(`heap:${PackumentCache.#heapLimit} maxSize:${maxSize} maxEntrySize:${maxEntrySize}`) |
| 55 | + } |
| 56 | + |
| 57 | + set (k, v, ...args) { |
| 58 | + // we use disposed only for a logging signal if we are setting packuments that |
| 59 | + // have already been evicted from the cache previously. logging here could help |
| 60 | + // us tune this in the future. |
| 61 | + const disposed = this.#disposed.has(k) |
| 62 | + /* istanbul ignore next - this doesnt happen consistently so hard to test without resorting to unit tests */ |
| 63 | + if (disposed) { |
| 64 | + this.#disposed.delete(k) |
| 65 | + } |
| 66 | + this.#log(k, 'set', `size:${v[this.#sizeKey]} disposed:${disposed}`) |
| 67 | + return super.set(k, v, ...args) |
| 68 | + } |
| 69 | + |
| 70 | + has (k, ...args) { |
| 71 | + const has = super.has(k, ...args) |
| 72 | + this.#log(k, `cache-${has ? 'hit' : 'miss'}`) |
| 73 | + return has |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +module.exports = PackumentCache |
0 commit comments