-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (142 loc) · 4.05 KB
/
index.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
var os = require('os'),
_ = require('lodash');
function SmartLRU(options) {
this.options = _.extend({
inactiveTTL: 0,
maxSize: '50m',
maxKeys: 0
}, options);
this.cache = {};
this.cacheSize = 0;
this.cacheLength = 0;
if(typeof(this.options.maxSize) === 'number') {
this.options.maxSize += 'b';
}
// If we can't match the regex then default to 80% of total memory
var maxSize = this.options.maxSize.match(/(\d+)([bkmg%])/i) || ['', '80', '%'],
size = +maxSize[1];
unit = maxSize[2].toLowerCase();
if(unit === 'b') {
this.maxSize = size;
} else if(unit === 'k') {
this.maxSize = size * 1024;
} else if(unit === 'm') {
this.maxSize = size * 1048576;
} else if(unit === 'g') {
this.maxSize = size * 1073741824;
} else if(unit === '%') {
if(size > 100) {
size = 100;
} else if(size <= 0) {
size = 1;
}
this.maxSize = (size / 100) * os.totalmem();
}
}
_.extend(SmartLRU.prototype, {
/**
* Evict Least Recently Used keys to bring the cache back within its given limits
*/
_evictKeys: function() {
var maxKeys = this.options.maxKeys;
while(this.cacheSize > this.maxSize || (maxKeys && this.cacheLength > maxKeys)) {
// Remove the first enumerated key. In practice, this will always be the oldest existing key.
// Oldest data goes first.
for(var key in this.cache) {
this.del(key);
--this.cacheLength;
break;
}
}
},
hasKey: function(key) {
return this.cache.hasOwnProperty(key);
},
set: function(key, value, ttl) {
var that = this,
value = Buffer.isBuffer(value) ? value : JSON.stringify(value),
storeSize = Buffer.byteLength(key) + Buffer.byteLength(value);
ttl = (ttl !== 0 ? (ttl || this.options.inactiveTTL) : 0);
// The key may have a timeout and it's also in the wrong place in the object if we just replace it. It must be deleted first.
if(this.cache[key]) {
this.del(key);
}
if(ttl) {
this.cache[key] = {
data: value,
size: storeSize,
ttl: ttl,
expire: setTimeout(function() {
that.del(key);
}, ttl)
};
} else {
this.cache[key] = {
data: value,
size: storeSize
};
}
++this.cacheLength;
this.cacheSize += storeSize;
this._evictKeys();
},
get: function(key, touch) {
if(this.cache[key]) {
if(!_.isBoolean(touch) || touch) {
this.touch(key);
}
var data = this.cache[key].data;
return Buffer.isBuffer(data) ? data : JSON.parse(data);
}
},
touch: function(key) {
var that = this,
cacheObj = this.cache[key];
if(cacheObj) {
// Bump the timeout
if(cacheObj.ttl) {
clearTimeout(cacheObj.expire);
cacheObj.expire = setTimeout(function() {
that.del(key);
}, cacheObj.ttl);
}
// Push to end of "queue" (object fur realz) - we don't want recently used data getting deleted when cache size exceeds limit
delete this.cache[key];
this.cache[key] = cacheObj;
return true;
}
return false;
},
/**
* Update a key's value without changing its TTL
*/
update: function(key, value) {
var cachedObj = this.cache[key],
value = Buffer.isBuffer(value) ? value : JSON.stringify(value);
if(!cachedObj) {
throw new Error("Failed to update value: Key '" + key + "' was not found in the cache");
}
var newSize = Buffer.byteLength(key) + Buffer.byteLength(value),
sizeDiff = newSize - cachedObj.size;
cachedObj.size = newSize;
cachedObj.data = value;
this.cacheSize += sizeDiff;
},
del: function(key) {
if(this.cache[key]) {
clearTimeout(this.cache[key].expire);
this.cacheSize -= this.cache[key].size;
delete this.cache[key];
--this.cacheLength;
}
},
reset: function() {
for(var key in this.cache) {
clearTimeout(this.cache[key].expire);
}
this.cache = {};
this.cacheSize = 0;
this.cacheLength = 0;
}
});
module.exports = SmartLRU;