@@ -50,45 +50,33 @@ class PackumentCache extends LRUCache {
5050
5151 constructor ( { heapFactor = 0.25 , maxEntryFactor = 0.5 , sizeKey = '_contentLength' } = { } ) {
5252 const maxSize = Math . floor ( PackumentCache . #heapLimit * heapFactor )
53+ const maxEntrySize = maxSize * maxEntryFactor
5354 super ( {
5455 maxSize,
55- maxEntrySize : maxSize * maxEntryFactor ,
56- sizeCalculation : ( p ) => {
57- // I saw some requests without a content length once but can't reproduce anymore
58- // lru cache will error if sizeCalculation isnt a positive number
59- if ( p [ sizeKey ] ) {
60- return p [ sizeKey ]
61- }
62- // Get the current average size of packuments in the cache
63- // Won't work if cache is empty or no items have a size
64- if ( this . calculatedSize && this . size ) {
65- return this . calculatedSize / this . size
66- }
67- // Some very wrong random guess at global average packument size
68- return 1_000_000
69- } ,
56+ maxEntrySize,
57+ // Don't cache if we dont know the size
58+ sizeCalculation : ( p ) => p [ sizeKey ] || maxEntrySize + 1 ,
7059 dispose : ( v , k ) => {
7160 this . #disposed. add ( k )
72- this . #log( 'dispose' , k )
61+ this . #log( k , 'dispose' )
7362 } ,
7463 } )
7564 this . #sizeKey = sizeKey
65+ this . #log( `heap:${ PackumentCache . #heapLimit} maxSize:${ maxSize } maxEntrySize:${ maxEntrySize } ` )
7666 }
7767
7868 set ( k , v , ...args ) {
79- if ( this . #disposed. has ( k ) ) {
69+ const disposed = this . #disposed. has ( k )
70+ if ( disposed ) {
8071 this . #disposed. delete ( k )
81- this . #log( 'set disposed' , k )
82- }
83- if ( ! v [ this . #sizeKey] ) {
84- this . #log( 'no size' , k )
8572 }
73+ this . #log( k , 'set' , `size:${ v [ this . #sizeKey] } disposed:${ disposed } ` )
8674 return super . set ( k , v , ...args )
8775 }
8876
8977 has ( k , ...args ) {
9078 const has = super . has ( k , ...args )
91- this . #log( `cache-${ has ? 'hit' : 'miss' } ` , k )
79+ this . #log( k , `cache-${ has ? 'hit' : 'miss' } ` )
9280 return has
9381 }
9482}
0 commit comments