-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.js
311 lines (260 loc) · 8.4 KB
/
cache.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
304
305
306
307
308
309
310
311
// Main Cache Interface
const Promise = require('bluebird');
const hash = require('object-hash');
const moment = require('moment');
const _ = require('lodash');
const logger = require('./logger');
const Value = require('./value');
const NotFoundError = require('./errors/notFound');
const defaultOptions = {
name: 'default',
bypassHeader: 'cache-bypass',
statusHeader: 'cache-status',
includeMethods: ['GET']
};
/**
* A cache object
*
* Takes in an array of stores and a hash of options
**/
function Cache(stores = [], options = {}) {
if(!(stores instanceof Array)) {
stores = [stores];
}
this.stores = stores;
this.options = Object.assign({},
defaultOptions,
options);
};
// Returns the list of stores
Cache.prototype.getStores = function getStores() {
return this.stores;
};
// A key generator based on [object-hash](https://github.com/puleos/object-hash)
Cache.prototype.getKey = function getKey(obj = {}) {
return this.options.name + ':cache:' + hash(obj);
};
// Sets a value in all stores, no manipulation of value at all
const multiSet = function(stores, key, value) {
return Promise.map(stores,
function(store) {
return store.set(key, value);
})
.then(function() {
return value;
})
.catch(err => {
logger.error(err);
return value;
});
};
Cache.prototype.set = function set(key, value) {
return multiSet(this.stores, key, value);
};
const processSearchResult = function(search) {
// If we found it, return
if(search.found) {
return search.value;
} else {
// Otherwise throw a not found
throw new NotFoundError(search.key);
}
};
// Returns raw data out of the stores, repopulating stores that don't have the
// data from ones that do.
Cache.prototype.get = function get(key) {
let self = this;
// Accumulator object
let search = {
found: false,
stores: [],
key: key
};
return Promise.reduce(
this.stores,
function(search, store) {
// If we've found the item already, just return
if(search.found) {
return search;
}
// Otherwise check this store
return store.get(key).then(function(value) {
// After this function returns,
// set this value in the failed stores
process.nextTick(function() {
self.lastPromise = multiSet(search.stores, key, value);
});
// We found it!
search.found = true;
search.value = value;
return search;
}).catch(NotFoundError, () => {
// Item not found, just keep looking
search.stores.push(store);
return search;
}).catch(err => {
// Error! Log it, and then keep looking
logger.error(err);
search.stores.push(store);
return search;
});
},
search)
.then(processSearchResult);
};
// Puts the raw data into a Value object and then sets it in the stores
Cache.prototype.createValueAndMultiSet = function(key, data, opts = {}) {
let value = new Value(data);
opts = Object.assign({}, this.options, opts);
value.setStaleTTL(opts.staleTTL);
value.setExpireTTL(opts.expireTTL);
return multiSet(this.stores, key, JSON.stringify(value))
.then(function() {
return value;
});
};
// Calls the function, storing the response
Cache.prototype.refresh = function(key, func, options = {}) {
let self = this;
return Promise.try(func).then(function(data) {
return self.createValueAndMultiSet(key, data, options);
}).then(function(value) {
return value.get();
});
};
// Wraps a function with the caching handling stale and expired states correctly
Cache.prototype.wrap = function wrap(key, func, options = {}) {
let self = this;
key = this.getKey(key);
// First try and get the key
return this.get(key).then(function(raw) {
let value = Value.fromJSON(raw);
if(value.expired()) {
// Expire values wait for us to get them again, throwing errors
return self.refresh(key, func, options);
} else if(value.stale()) {
// Stale values update on next tick
process.nextTick(function() {
self.lastPromise = Promise.try(func).then(function(data) {
return self.createValueAndMultiSet(key, data, options);
}).catch(function(err) {
// Error getting new value... do nothing
});
});
}
// As long as not expired, return it!
return value.get();
}).catch(function(err) {
// We couldn't find the value in the cache.
// Run the function and then set it
return self.refresh(key, func, options);
});
};
const addContent = function addContent(cache, content, encoding) {
if (Buffer.isBuffer(content)) {
var oldContent = Buffer.from(cache.content || '');
cache.content = Buffer.concat([oldContent, content], oldContent.length + content.length);
} else {
if(typeof content !== "undefined") {
cache.content = (cache.content || '') + content;
}
}
cache.encoding = encoding || cache.encoding;
};
// A connect middleware that supports stale and expired correctly
Cache.prototype.middleware = function middleware(opts = {}) {
let self = this;
opts = Object.assign({}, this.options, opts);
return function(req, res, next) {
// Bypass if we supplied header
if(req.headers[opts.bypassHeader]) {
res.setHeader(opts.statusHeader, 'bypass');
return next();
}
if(opts.includeMethods.indexOf(req.method) === -1) {
res.setHeader(opts.statusHeader, 'skipMethod');
return next();
}
let key = self.getKey({ url: req.originalUrl });
res._cache = {
write: res.write.bind(res),
end: res.end.bind(res),
getHeader: res.getHeader.bind(res),
removeHeader: res.removeHeader.bind(res),
setHeader: res.setHeader.bind(res),
encoding: undefined,
content: undefined,
headers: [],
stale: false,
expired: false
};
return self.get(key).then(function(raw) {
let value = Value.fromJSON(raw);
if(value.expired()) {
res._cache.expired = true;
throw new Error("Value expired");
}
let cached = value.get();
let data = cached.content;
if (typeof data !== "string" && data) {
data = Buffer.from(data.data);
}
_.forEach(cached.headers, ([name, value]) => res.setHeader(name, value));
res.setHeader('Cache-Control', value.getCacheControl());
res.setHeader(opts.statusHeader, 'cached');
res.writeHead(cached.status);
res.end(data);
if(value.stale()) {
res._cache.stale = true;
throw new Error("Value stale");
}
}).catch(function(err) {
res.getHeader = function(name) {
let header = _.find(res._cache.headers,
([key, value]) => key === name);
if(header) {
return header[1];
}
};
res.removeHeader = function(name) {
res._cache.headers = _.filter(res._cache.headers,
([key, value]) => key !== name);
}
res.setHeader = function(name, value) {
res.removeHeader(name);
res._cache.headers.push([name, value]);
};
// patch res.write
res.write = function(content, encoding) {
addContent(res._cache, content, encoding);
};
// patch res.end
res.end = function(content, encoding) {
addContent(res._cache, content, encoding);
// Save the content and headers
let data = Object.assign({}, {
content: res._cache.content,
headers: res._cache.headers,
status: res.statusCode
});
return self.createValueAndMultiSet(key, data, opts)
.then(function(value) {
if(!res._cache.stale) {
_.forEach(res._cache.headers, function(args) {
res._cache.setHeader.apply(res, args);
});
res._cache.setHeader.apply(res, ['Cache-Control', value.getCacheControl()]);
// Add Status Header
res._cache.setHeader.apply(res, [opts.statusHeader, res._cache.expired ? 'expired' : 'miss']);
if(typeof res._cache.content !== "undefined") {
res._cache.write.apply(res, [res._cache.content, res._cache.encoding]);
}
return res._cache.end.apply(this);
}
});
};
return next();
});
};
};
module.exports = Cache;