-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
271 lines (224 loc) · 6.69 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
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
/*!
* nano-cache
* Copyright (c) 2017 Cxense Inc
* Authors: [email protected], [email protected]
* MIT license https://opensource.org/licenses/MIT
*/
var extend = require("extend");
var zlib = require('zlib');
var os = require('os');
var EventEmitter = require('events').EventEmitter;
var NanoCache = function (options) {
this.init(options);
};
NanoCache.SIZE = {
GB : Math.pow(2, 30),
MB : Math.pow(2, 20),
KB : Math.pow(2, 10)
};
NanoCache.DEFAULTS = {
ttl: null, // msec
limit: null, // hits
bytes: Infinity,
compress: true,
minFreeMem : 0,
maxEvictBytes : os.totalmem() * .05
};
NanoCache.prototype = extend(true, {}, Object.create(EventEmitter.prototype), {
init : function (opt) {
this.options = extend({}, NanoCache.DEFAULTS, opt);
this.hits = 0;
this.evictions = 0;
this.misses = 0;
this._lastAccess = [];
this.clear();
},
get: function (key) {
this._checkExpired(key);
var datum = this._data[key];
if (!datum) {
this.misses++;
return null;
}
var value = this._value(key);
this.hits++;
datum.hits++;
datum.accessed = this.now();
this._updateActiveIndex(datum.key);
this.asyncExpireCheck();
this.emit('get', key);
return value;
},
_updateActiveIndex : function (key) {
this._removeFromActive(key);
this._lastAccess.push(key);
},
_removeFromActive : function (key){
var la = this._lastAccess;
for(var i = la.length - 1; i >= 0; i--){
if(la[i] === key){
la.splice(i, 1);
break;
}
}
},
asyncExpireCheck : function () {
var self = this;
clearTimeout(this._asyncCheck);
this._asyncCheck = setTimeout(function () {
self._asyncCheck = null;
self.clearExpired();
}, 0);
},
set: function (key, value, options) {
var opt = extend({}, this.options, options);
this.del(key);
var epoch = this.now();
var json = JSON.stringify(value);
var store_value = opt.compress
? zlib.deflateRawSync(json)
: json;
var store_buffer = Buffer.from(store_value);
var bytes = Buffer.byteLength(store_buffer, 'utf8');
var datum = {
key: key,
hits : 0,
accessed : epoch,
updated : epoch,
expires : null,
value : store_buffer,
bytes : bytes,
ttl: opt.ttl,
compressed: opt.compress,
cost: opt.cost || 1,
limit: opt.limit
};
this._data[key] = datum;
this.bytes += datum.bytes;
var ttl = parseInt(datum.ttl, 10);
if (!isNaN(ttl)) {
datum.expires = epoch + ttl;
}
if (opt.expires instanceof Date) {
opt.expires = opt.expires.getTime();
}
if (opt.expires > 0) {
datum.expires = opt.expires;
}
this._updateActiveIndex(datum.key);
this._checkLimits();
this.emit('set', key);
return value;
},
info : function (key) {
var datum = this._data[key];
if (!datum) {
return null;
}
return extend({}, datum, {
value: this._value(key)
});
},
_value : function (key) {
var datum = this._data[key];
if (!datum.value) {
return null;
}
var value = (datum.compressed)
? zlib.inflateRawSync(datum.value)
: datum.value;
return datum && JSON.parse(value);
},
del: function (key) {
var info = this.info(key);
if (!info) {
return null;
}
this.bytes -= info.bytes;
delete this._data[key];
this._removeFromActive(key);
this.emit('del', key);
return info.value;
},
clear: function () {
this._data = {};
this.bytes = 0;
this.emit('clear');
},
clearExpired: function () {
Object.keys(this._data).forEach(this._checkExpired.bind(this));
},
_checkExpired : function (key) {
if (this.isExpired(key)) {
this.del(key);
}
},
_checkLimits : function () {
this.clearExpired();
if (this.options.maxBytes) {
this._doEviction(function () {
var stats = this.stats();
return stats.bytes > this.options.maxBytes;
}.bind(this));
}
// check hard memory constraints
this._doEviction(function () {
return os.freemem() < this.options.minFreeMem;
}.bind(this));
// manual garbage collection can be enabled with `node --expose-gc`
if( global.gc ){
global.gc();
}
},
isExpired : function (key) {
return this.isTTLExpired(key) || this.isLimitReached(key);
},
isTTLExpired: function (key) {
var datum = this._data[key];
return datum && datum.expires > 0 && datum.expires <= this.now();
},
isLimitReached: function (key) {
var datum = this._data[key];
return datum && datum.limit > 0 && datum.limit <= datum.hits;
},
now : function () {
return (new Date()).getTime();
},
stats : function () {
var oldest = this._data[this._lastAccess[0]];
return {
count: this._lastAccess.length,
age : oldest && this.now() - oldest.accessed,
hits : this.hits,
evictions: this.evictions,
misses : this.misses,
bytes: this.bytes
};
},
_doEviction : function (callback) {
var keepGoing = callback();
if (!keepGoing) {
return;
}
var sorted = this._lastAccess;
var maxEvictBytes = this.options.maxEvictBytes;
var bytes;
while (keepGoing && sorted.length && maxEvictBytes > 0 ) {
var key = sorted.shift();
bytes = this._data[key].bytes;
maxEvictBytes -= bytes;
this.evictions++;
this.del( key );
keepGoing = callback();
}
}
});
// make it usable even without creating an instance of it.
// basically creating an instance, then copying all non-underscore-starting-functions to the factory
NanoCache.singleton = new NanoCache();
Object.keys(NanoCache.prototype).forEach(function (key) {
if (typeof NanoCache.singleton[key] === 'function' && key.indexOf('_') !== 0) {
NanoCache[key] = NanoCache.prototype[key].bind(NanoCache.singleton);
}
});
module.exports = NanoCache;