Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
;(function () { // closure for web browsers
2
3
if (typeof module === 'object' && module.exports) {
4
module.exports = LRUCache
5
} else {
6
// just set the global for non-node platforms.
7
this.LRUCache = LRUCache
8
}
9
10
function hOP (obj, key) {
11
return Object.prototype.hasOwnProperty.call(obj, key)
12
}
13
14
function naiveLength () { return 1 }
15
16
function LRUCache (options) {
17
if (!(this instanceof LRUCache))
18
return new LRUCache(options)
19
20
if (typeof options === 'number')
21
options = { max: options }
22
23
if (!options)
24
options = {}
25
26
this._max = options.max
27
// Kind of weird to have a default max of Infinity, but oh well.
28
if (!this._max || !(typeof this._max === "number") || this._max <= 0 )
29
this._max = Infinity
30
31
this._lengthCalculator = options.length || naiveLength
32
if (typeof this._lengthCalculator !== "function")
33
this._lengthCalculator = naiveLength
34
35
this._allowStale = options.stale || false
36
this._maxAge = options.maxAge || null
37
this._dispose = options.dispose
38
this.reset()
39
}
40
41
// resize the cache when the max changes.
42
Object.defineProperty(LRUCache.prototype, "max",
43
{ set : function (mL) {
44
if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
45
this._max = mL
46
if (this._length > this._max) trim(this)
47
}
48
, get : function () { return this._max }
49
, enumerable : true
50
})
51
52
// resize the cache when the lengthCalculator changes.
53
Object.defineProperty(LRUCache.prototype, "lengthCalculator",
54
{ set : function (lC) {
55
if (typeof lC !== "function") {
56
this._lengthCalculator = naiveLength
57
this._length = this._itemCount
58
for (var key in this._cache) {
59
this._cache[key].length = 1
60
}
61
} else {
62
this._lengthCalculator = lC
63
this._length = 0
64
for (var key in this._cache) {
65
this._cache[key].length = this._lengthCalculator(this._cache[key].value)
66
this._length += this._cache[key].length
67
}
68
}
69
70
if (this._length > this._max) trim(this)
71
}
72
, get : function () { return this._lengthCalculator }
73
, enumerable : true
74
})
75
76
Object.defineProperty(LRUCache.prototype, "length",
77
{ get : function () { return this._length }
78
, enumerable : true
79
})
80
81
82
Object.defineProperty(LRUCache.prototype, "itemCount",
83
{ get : function () { return this._itemCount }
84
, enumerable : true
85
})
86
87
LRUCache.prototype.forEach = function (fn, thisp) {
88
thisp = thisp || this
89
var i = 0
90
var itemCount = this._itemCount
91
92
for (var k = this._mru - 1; k >= 0 && i < itemCount; k--) if (this._lruList[k]) {
93
i++
94
var hit = this._lruList[k]
95
if (isStale(this, hit)) {
96
del(this, hit)
97
if (!this._allowStale) hit = undefined
98
}
99
if (hit) {
100
fn.call(thisp, hit.value, hit.key, this)
101
}
102
}
103
}
104
105
LRUCache.prototype.keys = function () {
106
var keys = new Array(this._itemCount)
107
var i = 0
108
for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
109
var hit = this._lruList[k]
110
keys[i++] = hit.key
111
}
112
return keys
113
}
114
115
LRUCache.prototype.values = function () {
116
var values = new Array(this._itemCount)
117
var i = 0
118
for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
119
var hit = this._lruList[k]
120
values[i++] = hit.value
121
}
122
return values
123
}
124
125
LRUCache.prototype.reset = function () {
126
if (this._dispose && this._cache) {
127
for (var k in this._cache) {
128
this._dispose(k, this._cache[k].value)
129
}
130
}
131
132
this._cache = Object.create(null) // hash of items by key
133
this._lruList = Object.create(null) // list of items in order of use recency
134
this._mru = 0 // most recently used
135
this._lru = 0 // least recently used
136
this._length = 0 // number of items in the list
137
this._itemCount = 0
138
}
139
140
// Provided for debugging/dev purposes only. No promises whatsoever that
141
// this API stays stable.
142
LRUCache.prototype.dump = function () {
143
return this._cache
144
}
145
146
LRUCache.prototype.dumpLru = function () {
147
return this._lruList
148
}
149
150
LRUCache.prototype.set = function (key, value, maxAge) {
151
maxAge = maxAge || this._maxAge
152
var now = maxAge ? Date.now() : 0
153
154
if (hOP(this._cache, key)) {
155
// dispose of the old one before overwriting
156
if (this._dispose)
157
this._dispose(key, this._cache[key].value)
158
159
this._cache[key].now = now
160
this._cache[key].maxAge = maxAge
161
this._cache[key].value = value
162
this.get(key)
163
return true
164
}
165
166
var len = this._lengthCalculator(value)
167
var hit = new Entry(key, value, this._mru++, len, now, maxAge)
168
169
// oversized objects fall out of cache automatically.
170
if (hit.length > this._max) {
171
if (this._dispose) this._dispose(key, value)
172
return false
173
}
174
175
this._length += hit.length
176
this._lruList[hit.lu] = this._cache[key] = hit
177
this._itemCount ++
178
179
if (this._length > this._max)
180
trim(this)
181
182
return true
183
}
184
185
LRUCache.prototype.has = function (key) {
186
if (!hOP(this._cache, key)) return false
187
var hit = this._cache[key]
188
if (isStale(this, hit)) {
189
return false
190
}
191
return true
192
}
193
194
LRUCache.prototype.get = function (key) {
195
return get(this, key, true)
196
}
197
198
LRUCache.prototype.peek = function (key) {
199
return get(this, key, false)
200
}
201
202
LRUCache.prototype.pop = function () {
203
var hit = this._lruList[this._lru]
204
del(this, hit)
205
return hit || null
206
}
207
208
LRUCache.prototype.del = function (key) {
209
del(this, this._cache[key])
210
}
211
212
function get (self, key, doUse) {
213
var hit = self._cache[key]
214
if (hit) {
215
if (isStale(self, hit)) {
216
del(self, hit)
217
if (!self._allowStale) hit = undefined
218
} else {
219
if (doUse) use(self, hit)
220
}
221
if (hit) hit = hit.value
222
}
223
return hit
224
}
225
226
function isStale(self, hit) {
227
if (!hit || (!hit.maxAge && !self._maxAge)) return false
228
var stale = false;
229
var diff = Date.now() - hit.now
230
if (hit.maxAge) {
231
stale = diff > hit.maxAge
232
} else {
233
stale = self._maxAge && (diff > self._maxAge)
234
}
235
return stale;
236
}
237
238
function use (self, hit) {
239
shiftLU(self, hit)
240
hit.lu = self._mru ++
241
self._lruList[hit.lu] = hit
242
}
243
244
function trim (self) {
245
while (self._lru < self._mru && self._length > self._max)
246
del(self, self._lruList[self._lru])
247
}
248
249
function shiftLU (self, hit) {
250
delete self._lruList[ hit.lu ]
251
while (self._lru < self._mru && !self._lruList[self._lru]) self._lru ++
252
}
253
254
function del (self, hit) {
255
if (hit) {
256
if (self._dispose) self._dispose(hit.key, hit.value)
257
self._length -= hit.length
258
self._itemCount --
259
delete self._cache[ hit.key ]
260
shiftLU(self, hit)
261
}
262
}
263
264
// classy, since V8 prefers predictable objects.
265
function Entry (key, value, lu, length, now, maxAge) {
266
this.key = key
267
this.value = value
268
this.lu = lu
269
this.length = length
270
this.now = now
271
if (maxAge) this.maxAge = maxAge
272
}
273
274
})()
275
276