/
index.js
276 lines (224 loc) · 7.74 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
272
273
274
275
276
var events = require("events");
var util = require("util");
/*
* This class a key/value cache on memory.
* A timeout will be added to each entry.
* When a timeout happens for a single entry, the event 'expired'
* will be rised with the pair key/value as argument.
* When an entry was query or updated, its timeout will be reseted.
* @param options {object} Optional configuration options.
* - timeout {number} Optional. Specifies in ms the default timeout for each entry. Default 60000 ms.
* - doesNotRenewTimeout {boolean} Optional. Specifies if entries's timeout should be reseted after each query or update. Default false.
* - timeoutDisabled {boolean} Optional. Enable/diable timeout feature. If timeout feature is desable, items will not expire. Default false.
* @api public
*/
var Cache = function(options) {
if (typeof options== 'number') {
options = { timeout: options};
} else {
options = options || {};
}
var self = this; // Self reference
var cache = {}; // Entries by key
var _length = 0; // Internal counter of cache's entries
var expirations = []; // Entries sorted ascending by their expiration time and sequence.
var sequence = 0; // Internal counter for sorting entries within 'expirations' array.
var timerId = null; // Reference to timer
var config = { // Global configuration
timeout: options.timeout || 60000,
doesNotRenewTimeout: options.doesNotRenewTimeout || false,
timeoutDisabled: options.timeoutDisabled || false
};
/*
* Returns the number of entries that the cache contains.
* @api public
*/
Object.defineProperty(this, "length", {
enumerable: true,
get : function(){ return _length; }
});
/*
* Returns all keys.
* @api public
*/
Object.defineProperty(this, "keys", {
enumerable: true,
get : function(){ return Object.keys(cache); }
});
/*
* Inserts or updates an entry into the cache.
* @param key {string} Required.
* @param value {any} Required.
* @param timeout {number} Optional. Specifies in milliseconds the timeout for this entry.
* @api public
*/
this.set = function (key, value, timeout) {
var current = cache[key];
if (current) {
shouldRemoveExpiration(current);
} else {
_length++;
}
var item = {
key: key,
value: value
};
cache[key] = item;
if (!config.timeoutDisabled) {
item.timeout = timeout || config.timeout;
addExpiration(item);
}
};
/*
* Removes an entry from the cache.
* @param key {string} Required.
* @api public
*/
this.remove =function (key) {
var item = cache[key];
if (!item) return null;
_length --;
shouldRemoveExpiration(item);
delete cache[key];
return item.value;
};
/*
* Gets an entry's value by its key.
* @param key {string} Required.
* @return {any} Returns entry's value or null if entry was not found
* @api public
*/
this.get = function (key) {
var item = cache[key];
if (item) {
if (!config.timeoutDisabled && !config.doesNotRenewTimeout) {
removeExpiration(item);
addExpiration(item);
}
return item.value;
}
return null;
};
/*
* Removes all entries from the cache
* @public api
*/
this.clean = function () {
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
cache = {};
expirations = [];
_length = 0;
};
// no operation function
var noop = function() {};
// adds an entry to expirations array
var addExpiration = function (item) {
item.expires = new Date().getTime() + item.timeout;
item.sequence = sequence++;
var index = binaryInsert(item, itemComparer);
if (index === 0) setItemTimeout(item);
};
// removes an entry from expirations array
var removeExpiration = function (item) {
var index = binarySearch(item);
if (index >= 0) {
if (index === 0 && expirations.length > 1) setItemTimeout(expirations[1]);
expirations.splice(index, 1);
}
};
var shouldRemoveExpiration = config.timeoutDisabled ? noop : removeExpiration;
// sets expiration timer for an item
var setItemTimeout = function (item) {
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
var timeout = item.expires - new Date().getTime();
timerId = setTimeout(onTimer, timeout < 10 ? 10 : timeout);
};
// on timer event, emits one event 'expired' for each entry at expirations array that are expired.
var onTimer = function() {
var now = new Date().getTime();
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
var itemsToEmit = []; // Collects all expired items
for (var index in expirations) {
// Gets entry from expirations cache
var item = expirations[index];
// Stops when find a non expired item
if (item.expires > now) {
// Sets timer for no expired item
setItemTimeout(item);
// Removes all expired entries from array
expirations = expirations.slice(index);
break;
}
// All remaining expirations may need to be removed...
else if( index == expirations.length-1 && item.expires <= now) {
expirations = expirations.slice(index+1);
}
// Adds expired entry to collection of expired items
itemsToEmit.push(item);
// Removes expired entry from cache
delete cache[item.key];
}
// Updates length property
_length -= itemsToEmit.length;
// Emits 'expired' event for each expired item
itemsToEmit.forEach( function( item ) {
self.emit("expired", {
key: item.key,
value: item.value
});
});
};
// Internal function that compares two entries's timeouts
var itemComparer = function(a, b) {
if (a && !b) return -1;
if (!a && b) return 1;
if (b.expires === a.expires) {
return a.sequence - b.sequence;
}
return a.expires - b.expires;
};
// searchs on expirations array
var binarySearch = function (value) {
var low = 0, up = expirations.length, middle, result;
while ( low <= up ) {
middle = (low + up) >> 1;
result = itemComparer(value, expirations[middle]);
if (result === 0) return middle;
if (result > 0) {
low = middle + 1;
} else {
up = middle - 1;
}
}
return -1;
};
// inserts on expirations array
var binaryInsert = function (value) {
var low = 0, up = expirations.length, count = up, middle, result;
while ( low <= up ) {
middle = (low + up) >> 1;
result = itemComparer(value, expirations[middle]);
if (result === 0) return middle;
if (result > 0 ) {
low = middle + 1;
} else {
up = middle - 1;
}
}
expirations.splice(low, 0, value);
return low>count ? count : low;
};
};
// Cache inherits from EventEmitter
util.inherits(Cache, events.EventEmitter);
// Exports Cache class
module.exports = Cache;