-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (207 loc) · 7.38 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
"use strict";
const bluebird = require('bluebird');
const _ = require('lodash');
module.exports = class Cache {
constructor({ttl = 30000, garbageCollectorTimeInterval = 10000, garbageCollectorAsyncMode = false, debugMode = false} = {}) {
this.debugMode = debugMode;
this.storage = new Map();
if (Number.isInteger(ttl)) {
this.ttl = ttl;
} else {
throw new Error("lite-node-cache: ttl parameter is not valid");
}
this.elements = [];
if (Number.isInteger(garbageCollectorTimeInterval) && garbageCollectorTimeInterval >= 0) {
if (garbageCollectorTimeInterval !== 0) {
this.garbageCollectorTimeInterval = garbageCollectorTimeInterval;
if (garbageCollectorAsyncMode) {
this.garbageCollectorAsync();
}
else {
this.garbageCollector();
}
}
}
else {
throw new Error("lite-node-cache: garbageCollectorTimeInterval parameter is not valid");
}
this.garbageCollectorIsExecuted = 0;
}
get(key) {
if (this.storage.has(key)) {
const item = this.storage.get(key);
if(item.ttl !== 0){
const ttl = (item.ttl !== null) ? item.ttl : this.ttl;
if (Date.now() - item.created > ttl) {
this.delete(key);
this.debug("lite-node-cache: Removed the key when get. ttl elapsed.");
return false;
} else {
return item.value;
}
}else{
return item.value;
}
} else {
return false;
}
}
getAsync(key) {
return new bluebird(function (resolve, reject) {
setTimeout(()=> {
try {
resolve(this.get(key));
} catch (err) {
reject(err);
}
}, 0);
});
}
set(key, value, ttl = null) {
if(ttl !== null && ( !Number.isInteger(ttl) && ttl >= 0 )){
throw Error("lite-node-cache: ttl parameter is not valid in set method");
}
const has = this.storage.has(key);
const item = {
value,
created: Date.now(),
ttl
};
this.storage.set(key, item);
this.elements.push(key);
return has;
}
setAsync(key, value, ttl) {
return new bluebird(function (resolve, reject) {
setTimeout(()=> {
try {
resolve(this.set(key, value, ttl));
} catch (err) {
reject(err);
}
}, 0);
});
}
remove(key){
return this.delete(key);
}
delete(key) {
if(this.storage.delete(key)){
_.remove(this.elements, function functionName(item) {
return item === key;
});
return true;
}else{
return false;
}
}
mget(keys) {
const resultArray = [];
for (let i = 0; i < keys.length; i++) {
resultArray.push(this.get(keys[i]));
}
return resultArray;
}
mset(keys, values, ttl) {
const resultArray = [];
for (let i = 0; i < keys.length; i++) {
resultArray.push(this.set(keys[i], values[i], ttl[i]));
}
return resultArray;
}
keys(){
return this.elements.slice();
}
debug() {
if (this.debugMode) console.log.apply(this, arguments);
}
removeGarbage() {
let removedCount = 0;
for (let i = 0, length = this.elements.length; i < length; i++) {
const key = this.elements[i];
if (this.storage.has(key)) {
const item = this.storage.get(key);
if(item.ttl !== 0){
const ttl = ( item.ttl !== null) ? item.ttl : this.ttl;
if (Date.now() - item.created > ttl) {
this.delete(key);
length--;
removedCount++;
}
}
}
}
return removedCount;
}
garbageCollector() {
if (!this.garbageCollectorIsExecuted) {
this.garbageCollectorIsExecuted++;
this.debug("lite-node-cache: garbage collector started");
setInterval(()=> {
let removedCount = 0;
removedCount += this.removeGarbage();
if (removedCount)
this.debug("lite-node-cache: the garbage collector made a clean cache (removed " + removedCount + " items)");
}, this.garbageCollectorTimeInterval);
} else {
this.debug("lite-node-cache: the garbage collector is already running");
}
}
removeGarbageAsync(key) {
return new bluebird((resolve, reject)=> {
setTimeout(()=> {
try {
if (this.storage.has(key)) {
const item = this.storage.get(key);
if(item.ttl !== 0){
const ttl = ( item.ttl !== null ) ? item.ttl : this.ttl;
if (Date.now() - item.created > ttl) {
this.delete(key);
resolve(true);
} else {
resolve(false);
}
}else{
resolve(false);
}
} else {
resolve(false);
}
} catch (err) {
reject(err);
}
}, 0);
});
}
garbageCollectorAsync() {
if (!this.garbageCollectorIsExecuted) {
this.garbageCollectorIsExecuted++;
this.debug("lite-node-cache: garbage collector started in async mode");
const self = this;
bluebird.coroutine(function* GCLoop() {
try {
yield bluebird.delay(self.garbageCollectorTimeInterval);
const removedArray = [];
for (let i = 0; i < self.elements.length; i++) {
const item = yield self.removeGarbageAsync(self.elements[i], self).then(function (result) {
return result;
});
removedArray.push(item);
}
if (self.debugMode)
bluebird.coroutine(function* () {
let removedArrayResolved = yield bluebird.all(removedArray);
removedArrayResolved = _.compact(removedArrayResolved);
if (removedArrayResolved.length)
self.debug("lite-node-cache: the garbage collector made a clean cache (removed " + removedArrayResolved.length + " items)");
})();
yield bluebird.coroutine(GCLoop)();
} catch (err) {
self.debug("lite-node-cache: " + err);
}
})();
} else {
this.debug("lite-node-cache: the garbage collector is already running");
}
}
};