-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlucache.js
More file actions
53 lines (46 loc) · 854 Bytes
/
Copy pathlucache.js
File metadata and controls
53 lines (46 loc) · 854 Bytes
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
function make(maxSize){
var keys = [];
var m = {};
var counts = {};
var size = 0;
var hooks = [];
var lowerCountBound = 0;
function evict(){
var min = 4294967295;
var minIndex;
for(var i=0;i<keys.length;++i){
var key = keys[i];
var c = counts[key];
if(c < min){
min = c;
minIndex = i;
}
}
var key = keys[minIndex];
delete counts[key];
delete m[key];
keys.splice(minIndex, 1);
lowerCountBound = min;
}
var h = {
use: function(key, value){
var c = counts[key];
if(c === undefined){
if(size < maxSize || Math.random() < 1/(1+lowerCountBound)){
if(size === maxSize) evict();
else ++size;
counts[key] = 1;
m[key] = value;
keys.push(key);
}
}else{
++counts[key];
}
},
get: function(key){
return m[key];
}
};
return h;
}
exports.make = make;