-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-lru.js
312 lines (276 loc) · 6.81 KB
/
simple-lru.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
;(function () {
'use strict';
/**
* export SimpleLRU for CommonJS, AMD
*/
if (typeof define === "function" && define.amd) {
define(function () {
return SimpleLRU;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = SimpleLRU;
} else if (typeof window !== 'undefined') {
window.SimpleLRU = SimpleLRU;
}
SimpleLRU.version = '0.0.3';
/**
* Simple mixin utility
* @api private
*/
function extend(obj1, obj2) {
for (var key in obj2) obj1[key] = obj2[key];
}
var nativeCreate = Object.create;
/**
* Object data store
* this is a `Object.create` fallback in order to support IE8
* @api private
*/
var Data = (function () {
var Data, proto;
if (typeof nativeCreate === 'function') {
Data = function () {
this.data = nativeCreate(null);
};
proto = {
get: function (key) {
return this.data[key];
},
has: function (key) {
return !! this.data[key];
}
};
} else {
Data = function () {
this.data = {};
};
proto = {
get: function (key) {
if (this.has(key)) return this.data[key];
},
has: function (key) {
return Object.prototype.hasOwnProperty.call(this.data, key);
}
};
}
extend(proto, {
set: function (key, val) {
this.data[key] = val;
},
del: function (key) {
var val = this.get(key);
if (typeof val !== 'undefined') {
delete this.data[key];
return val;
}
}
});
extend(Data.prototype, proto);
return Data;
})();
/**
* Cache entry instance
*
* @param {String}
* @param {any}
* @param {Number}
* @api private
*/
function Entry(key, val, index) {
this.key = key;
this.val = val;
this.index = index;
}
/**
* SimpleLRU constructor
* It holds following private properties. See `#reset()`
*
* _byKey {Data} map by key
* _byOrder {Object} map by recently used order
* _head {Number} index of next entry
* _tail {Number} index of least recently used cache item
* _len {Number} total number of cache items
*
* `_tail` is an index of the least recently used cache item.
* `_head` is an index of the most recently used cache item *plus* one.
*
* @param {Number} max length of cache item
*/
function SimpleLRU(max) {
if (typeof max !== 'number') throw new TypeError('max is requried');
this.max(max);
this.reset();
}
extend(SimpleLRU.prototype, {
/**
* Set cache by key
* @param {String} unique string key
* @param {String|Object|Number} any value
*/
set: function (key, val) {
var entry = this._byKey.get(key);
// reuse entry if the key exists
if (entry) {
this._touch(entry);
entry.val = val;
return;
}
entry = new Entry(key, val, this._head++);
this._byKey.set(key, entry);
this._byOrder[entry.index] = entry;
this._len++;
this._trim();
},
/**
* delete cache by key
*
* @param {String}
* @return {String|Object|Number} cached value
*/
del: function (key) {
var entry = this._byKey.del(key);
if (!entry) return;
delete this._byOrder[entry.index];
this._len--;
if (this._len === 0) {
this._head = this._tail = 0;
} else {
// update most index if it was most lecently used entry
if (entry.index === this._head - 1) this._pop();
// update least index if it was least lecently used entry
if (entry.index === this._tail) this._shift();
}
return entry.val;
},
/**
* get cache by key
*
* @param {String}
* @return {any} cache if it exists
*/
get: function (key) {
var entry = this._byKey.get(key);
if (entry) {
this._touch(entry);
return entry.val;
}
},
/**
* get a cache by key without touching index
* @return {any}
*/
peek: function (key) {
var entry = this._byKey.get(key);
if (entry) return entry.val;
},
/**
* see if key is exists or not
* @return {Boolean}
*/
has: function (key) {
return this._byKey.has(key);
},
/**
* total number of cache
* @return {Number}
*/
length: function () {
return this._len;
},
/**
* clear all stored cache
*/
reset: function () {
this._byKey = new Data();
this._byOrder = nativeCreate ? nativeCreate(null) : {};
this._head = 0;
this._tail = 0;
this._len = 0;
},
/**
* Getter|Setter function of "max" option
* @param {Number} if setter
*/
max: function (max) {
if (typeof max !== 'number') return this._max;
if (max < 1) throw new TypeError('max should be a positive number');
var shrink = (this._max || 0) > max;
this._max = max;
if (shrink) this._trim();
},
/**
* return array of keys in least recently used order
* @return {Array}
*/
keys: function () {
var count = 0
, tail = this._tail
, head = this._head
, keys = new Array(this._len);
for (var i = tail; i < head; i++) {
var entry = this._byOrder[i];
if (entry) keys[count++] = entry.key;
}
return keys;
},
/**
* update least recently used index of an entry to "_head"
*
* @param {Entry}
* @api private
*/
_touch: function (entry) {
// update most number to key
if (entry.index !== this._head - 1) {
var isTail = entry.index === this._tail;
delete this._byOrder[entry.index];
entry.index = this._head++;
this._byOrder[entry.index] = entry;
if (isTail) this._shift();
}
},
/**
* trim entries
* @api private
*/
_trim: function () {
var max = this._max;
while (max < this._len) {
var tailEntry = this._byOrder[this._tail];
this.del(tailEntry.key);
}
},
/**
* update tail index
* @return {Entry|undefined}
* @api private
*/
_shift: function () {
var tail = this._tail
, head = this._head;
for (var i = tail; i < head; i++) {
var entry = this._byOrder[i];
if (entry) {
this._tail = i;
return entry;
}
}
},
/**
* update head index
* @return {Entry|undefined}
* @api private
*/
_pop: function () {
var tail = this._tail
, head = this._head;
for (var i = head - 1; i >= tail; i--) {
var headEntry = this._byOrder[i];
if (headEntry) {
this._head = i + 1;
return headEntry;
}
}
}
});
})();