@@ -3,7 +3,7 @@ const perf = typeof performance === 'object' && performance &&
3
3
4
4
const hasAbortController = typeof AbortController !== 'undefined'
5
5
6
- /* istanbul ignore next - minimal backwards compatibility polyfill */
6
+ // minimal backwards- compatibility polyfill
7
7
const AC = hasAbortController ? AbortController : Object . assign (
8
8
class AbortController {
9
9
constructor ( ) { this . signal = new AC . AbortSignal }
@@ -36,14 +36,20 @@ const deprecatedProperty = (field, instead) => {
36
36
}
37
37
}
38
38
39
- const shouldWarn = code => typeof process === 'object' &&
40
- process &&
41
- ! warned . has ( code )
39
+ const emitWarning = ( ...a ) => {
40
+ typeof process === 'object' &&
41
+ process &&
42
+ typeof process . emitWarning === 'function'
43
+ ? process . emitWarning ( ...a )
44
+ : console . error ( ...a )
45
+ }
46
+
47
+ const shouldWarn = code => ! warned . has ( code )
42
48
43
49
const warn = ( code , what , instead , fn ) => {
44
50
warned . add ( code )
45
51
const msg = `The ${ what } is deprecated. Please use ${ instead } instead.`
46
- process . emitWarning ( msg , 'DeprecationWarning' , code , fn )
52
+ emitWarning ( msg , 'DeprecationWarning' , code , fn )
47
53
}
48
54
49
55
const isPosInt = n => n && n === Math . floor ( n ) && n > 0 && isFinite ( n )
@@ -72,7 +78,10 @@ class ZeroArray extends Array {
72
78
73
79
class Stack {
74
80
constructor ( max ) {
75
- const UintArray = max ? getUintArray ( max ) : Array
81
+ if ( max === 0 ) {
82
+ return [ ]
83
+ }
84
+ const UintArray = getUintArray ( max )
76
85
this . heap = new UintArray ( max )
77
86
this . length = 0
78
87
}
@@ -92,6 +101,7 @@ class LRUCache {
92
101
ttlResolution = 1 ,
93
102
ttlAutopurge,
94
103
updateAgeOnGet,
104
+ updateAgeOnHas,
95
105
allowStale,
96
106
dispose,
97
107
disposeAfter,
@@ -136,7 +146,6 @@ class LRUCache {
136
146
throw new TypeError ( 'fetchMethod must be a function if specified' )
137
147
}
138
148
139
-
140
149
this . keyMap = new Map ( )
141
150
this . keyList = new Array ( max ) . fill ( null )
142
151
this . valList = new Array ( max ) . fill ( null )
@@ -170,6 +179,7 @@ class LRUCache {
170
179
171
180
this . allowStale = ! ! allowStale || ! ! stale
172
181
this . updateAgeOnGet = ! ! updateAgeOnGet
182
+ this . updateAgeOnHas = ! ! updateAgeOnHas
173
183
this . ttlResolution = isPosInt ( ttlResolution ) || ttlResolution === 0
174
184
? ttlResolution : 1
175
185
this . ttlAutopurge = ! ! ttlAutopurge
@@ -191,7 +201,7 @@ class LRUCache {
191
201
warned . add ( code )
192
202
const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
193
203
'result in unbounded memory consumption.'
194
- process . emitWarning ( msg , 'UnboundedCacheWarning' , code , LRUCache )
204
+ emitWarning ( msg , 'UnboundedCacheWarning' , code , LRUCache )
195
205
}
196
206
}
197
207
@@ -207,7 +217,7 @@ class LRUCache {
207
217
}
208
218
209
219
getRemainingTTL ( key ) {
210
- return this . has ( key ) ? Infinity : 0
220
+ return this . has ( key , { updateAgeOnHas : false } ) ? Infinity : 0
211
221
}
212
222
213
223
initializeTTLTracking ( ) {
@@ -292,7 +302,7 @@ class LRUCache {
292
302
this . sizes [ index ] = size
293
303
const maxSize = this . maxSize - this . sizes [ index ]
294
304
while ( this . calculatedSize > maxSize ) {
295
- this . evict ( )
305
+ this . evict ( true )
296
306
}
297
307
this . calculatedSize += this . sizes [ index ]
298
308
}
@@ -512,8 +522,8 @@ class LRUCache {
512
522
if ( this . size === 0 ) {
513
523
return this . tail
514
524
}
515
- if ( this . size === this . max ) {
516
- return this . evict ( )
525
+ if ( this . size === this . max && this . max !== 0 ) {
526
+ return this . evict ( false )
517
527
}
518
528
if ( this . free . length !== 0 ) {
519
529
return this . free . pop ( )
@@ -525,12 +535,12 @@ class LRUCache {
525
535
pop ( ) {
526
536
if ( this . size ) {
527
537
const val = this . valList [ this . head ]
528
- this . evict ( )
538
+ this . evict ( true )
529
539
return val
530
540
}
531
541
}
532
542
533
- evict ( ) {
543
+ evict ( free ) {
534
544
const head = this . head
535
545
const k = this . keyList [ head ]
536
546
const v = this . valList [ head ]
@@ -543,14 +553,29 @@ class LRUCache {
543
553
}
544
554
}
545
555
this . removeItemSize ( head )
556
+ // if we aren't about to use the index, then null these out
557
+ if ( free ) {
558
+ this . keyList [ head ] = null
559
+ this . valList [ head ] = null
560
+ this . free . push ( head )
561
+ }
546
562
this . head = this . next [ head ]
547
563
this . keyMap . delete ( k )
548
564
this . size --
549
565
return head
550
566
}
551
567
552
- has ( k ) {
553
- return this . keyMap . has ( k ) && ! this . isStale ( this . keyMap . get ( k ) )
568
+ has ( k , { updateAgeOnHas = this . updateAgeOnHas } = { } ) {
569
+ const index = this . keyMap . get ( k )
570
+ if ( index !== undefined ) {
571
+ if ( ! this . isStale ( index ) ) {
572
+ if ( updateAgeOnHas ) {
573
+ this . updateItemAge ( index )
574
+ }
575
+ return true
576
+ }
577
+ }
578
+ return false
554
579
}
555
580
556
581
// like get(), but without any LRU updating or TTL expiration
0 commit comments