-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
303 lines (239 loc) · 7.93 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'use strict';
var Medusa = (function() {
var cache = {};
var currentTasks = {};
var hOP = cache.hasOwnProperty;
var settings = {
debug: false,
returnMutator: false,
defaultProvider: 'memory',
retry: 5000,
};
var policyMaker = function(incPolicy) {
var outPolicy = {
expiry: false,
provider: settings.defaultProvider,
};
// Blank policy, false, or no policy. lets store forever
if (!incPolicy) {
return outPolicy;
}
// Type is a full policy object
if (typeof incPolicy === 'object' && incPolicy.expiry) {
outPolicy.expiry = incPolicy.expiry;
outPolicy.provider = incPolicy.provider || outPolicy.provider;
} else {
outPolicy.expiry = incPolicy;
}
// Date object parsing
if (outPolicy.expiry.getTime) {
var d = new Date();
outPolicy.expiry = Math.ceil((outPolicy.expiry.getTime() - d.getTime()) / 1000);
}
// Number is too small, negative or not a number
outPolicy.expiry = parseInt(outPolicy.expiry) > 0 ? parseInt(outPolicy.expiry) : false;
return outPolicy;
};
var medusaCore = {
// Placeholder for cache providers
providers: {},
// Alter settings for Medusa
settings: function(newSettings) {
if (!newSettings) {
return settings;
}
Object.assign(settings, newSettings);
return settings;
},
// Add a provider
addProvider: function(name, provider) {
provider.init(); // Trigger for provider to clear any old cache items or any other cleanup
medusaCore.providers[name] = provider;
},
// Allows you to get from the cache or pull from the promise
get: function(key, prom, policy) {
policy = policyMaker(policy);
var prov = medusaCore.providers[policy.provider];
return prov.get(key, prom, policy)
.then(function(val) {
if (settings.returnMutator) {
return settings.returnMutator(val);
}
return val;
})
.catch(function() {
if (hOP.call(currentTasks, key) && Array.isArray(currentTasks[key]) && currentTasks[key].length > 0) {
// Add to the current task, but ignore if any items is below retry anyway threshold
var oldQueue = false;
for (var i in currentTasks[key]){
if(currentTasks[key][i].queued < new Date(Date.now() - settings.retry)){
oldQueue = true;
}
}
if(!oldQueue){
var concurent = new Promise(function(resolve, reject) {
currentTasks[key].push({
res: resolve,
rej: reject,
queued: new Date(),
});
});
return concurent;
}
}else{
// Add current task to list
currentTasks[key] = [{queued: new Date()}];
}
return new Promise(function(resolve, reject) {
// The cached item does not exist, resolve, store and return
var resolveExt = function(v) {
medusaCore.put(key, v, policy);
if (settings.returnMutator) {
v = settings.returnMutator(v);
}
resolve(v);
if (hOP.call(currentTasks, key)) {
for (var i in currentTasks[key]) {
if(currentTasks[key][i].res){
currentTasks[key][i].res(v);
}
}
currentTasks[key] = [];
delete currentTasks[key];
}
};
var rejectExt = function(v){
reject(v);
if (hOP.call(currentTasks, key)) {
for (var i in currentTasks[key]) {
if(currentTasks[key][i].rej){
currentTasks[key][i].rej(v);
}
}
currentTasks[key] = [];
delete currentTasks[key];
}
};
prom(resolveExt, rejectExt);
});
});
},
// Allows you to get from the cache or pull from the promise
overwrite: function(key, prom, policy) {
return new Promise(function(resolve, reject) {
// Re-put the object no matter what
var resolveExt = function(v) {
medusaCore.put(key, v, policyMaker(policy)).then(function(val) {
if (settings.returnMutator) {
v = settings.returnMutator(val);
}
resolve(val);
});
};
prom(resolveExt, reject);
});
},
// Place an item into the cache
put: function(key, value, policy) {
policy = policyMaker(policy);
var prov = medusaCore.providers[policy.provider];
return prov.set(key, value, policy);
},
// Clear one or all items in the cache
clear: function(key, provider) {
var prov = medusaCore.providers[provider || settings.defaultProvider];
// Clear a wildcard search of objects
if (key && key.indexOf('*') > -1) {
return prov.keys().then(function(keys) {
var cacheMatchKeys = keys.filter(function(str) {
return new RegExp('^' + key.split('*').join('.*') + '$').test(str);
});
var clearPromises = cacheMatchKeys.map(prov.clear);
// Incase someone somehow used a wildcard in their cached key (don't do this)
clearPromises.push(prov.clear(key));
return Promise.all(clearPromises);
});
}
// Not a special clear
return prov.clear(key);
},
};
// Memory cache is about the simplist cache possible, use it as an example
var memoryCache = {
init: function() {
// This should be used to update the cache on boot,
// memory cache will be blank on boot by default
return;
},
get: function(key) {
// Gets the value as a promise, will resolve on found, will reject if not found
return new Promise(function(resolve, reject) {
if (hOP.call(cache, key) && cache[key].val) {
// The cached item exists, return it
resolve(cache[key].val);
} else {
// The cached item does not exist reject
reject(false);
}
});
},
set: function(key, value, policy) {
// Sets the value, returns a promise when the storage is complete, promise will resolve to the value
// Clear in case it exists
return memoryCache.clear(key)
.then(function() {
// Set a timemout to self-remove from the cache if in policy
var to = false;
if (policy && policy.expiry && policy.expiry > 0) {
to = setTimeout(function() {
memoryCache.clear(key);
}, policy.expiry);
}
// Store the cached item
cache[key] = {
policy: policy,
val: value,
to: to,
};
return value;
});
},
keys: function() {
// Return all keys for the storage as a promise
return new Promise(function(resolve) {
resolve(Object.keys(cache));
});
},
clear: function(key) {
return new Promise(function(resolve) {
// Clears a single key or complete clear on empty
// Clear all items in the cache
if (!key) {
for (var i in cache) {
memoryCache._clear(i);
}
resolve(true);
}
resolve(memoryCache._clear(key));
});
},
_clear: function(key) {
// Clear a single item, making sure to remove the extra timeout
if (hOP.call(cache, key)) {
if (cache[key].to) {
clearTimeout(cache[key].to);
}
cache[key] = null;
delete cache[key];
return true;
}
return false;
},
};
medusaCore.addProvider('memory', memoryCache);
return medusaCore;
})();
// CommonJS binding
if (typeof module === 'object' && module.exports) {
module.exports = Medusa;
}