-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce966cb
commit 21deba6
Showing
5 changed files
with
75 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { LRUCache } = require('lru-cache') | ||
const { getHeapStatistics } = require('node:v8') | ||
const { log } = require('proc-log') | ||
|
||
class PackumentCache extends LRUCache { | ||
static #heapLimit = Math.floor(getHeapStatistics().heap_size_limit) | ||
|
||
#sizeKey | ||
#disposed = new Set() | ||
|
||
#log (...args) { | ||
console.error('packumentCache', ...args) | ||
} | ||
|
||
constructor ({ | ||
heapFactor = 0.25, | ||
maxEntryFactor = 0.5, | ||
sizeKey = '_contentLength', | ||
} = {}) { | ||
const maxSize = Math.floor(PackumentCache.#heapLimit * heapFactor) | ||
const maxEntrySize = Math.floor(maxSize * maxEntryFactor) | ||
super({ | ||
maxSize, | ||
maxEntrySize, | ||
// Don't cache if we dont know the size | ||
sizeCalculation: (p) => p[sizeKey] || maxEntrySize + 1, | ||
dispose: (v, k) => { | ||
this.#disposed.add(k) | ||
this.#log(k, 'dispose') | ||
}, | ||
}) | ||
this.#sizeKey = sizeKey | ||
this.#log(`heap:${PackumentCache.#heapLimit} maxSize:${maxSize} maxEntrySize:${maxEntrySize}`) | ||
} | ||
|
||
set (k, v, ...args) { | ||
// we use disposed only for a logging signal if we are setting packuments that | ||
// have already been evicted from the cache previously. logging here could help | ||
// us tune this in the future. | ||
const disposed = this.#disposed.has(k) | ||
/* istanbul ignore next - this doesnt happen consistently so hard to test without resorting to unit tests */ | ||
if (disposed) { | ||
this.#disposed.delete(k) | ||
} | ||
this.#log(k, 'set', `size:${v[this.#sizeKey]} disposed:${disposed}`) | ||
return super.set(k, v, ...args) | ||
} | ||
|
||
has (k, ...args) { | ||
const has = super.has(k, ...args) | ||
this.#log(k, `cache-${has ? 'hit' : 'miss'}`) | ||
return has | ||
} | ||
} | ||
|
||
module.exports = PackumentCache | ||
// module.exports = Map |