-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (155 loc) · 4.92 KB
/
index.js
File metadata and controls
193 lines (155 loc) · 4.92 KB
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
'use strict';
// sigmund produces consistent hashes for complex nested objects,
// used here to generate cache keys from function name + arguments
var sigmund = require('sigmund');
var log = require('debug')('obcachejs');
var util = require('util');
function keygen(name, args) {
return sigmund({ f: name, a: args }, 8);
}
function CacheError(message) {
this.message = message || '';
Error.captureStackTrace(this, CacheError);
}
util.inherits(CacheError, Error);
CacheError.prototype.name = 'CacheError';
function filterArgs(args, skipArgs) {
if (!skipArgs || !skipArgs.length) return args;
return args.filter(function(a, i) {
return skipArgs.indexOf(i) === -1;
});
}
function validateCacheFunction(func) {
if (!func || typeof func !== 'function' || !func.cacheName) {
throw new Error('Not an obcachejs function');
}
}
var cache = {
Error: CacheError,
Create: function(options) {
options = options || {};
var nextResetTime;
var anonFnId = 0;
var store;
if (options.redis) {
log('creating redis cache');
store = require('./redis').init(options);
} else {
store = require('./lru').init(options);
}
this.store = store;
this.pending = options.queueEnabled ? {} : null;
this.stats = { hit: 0, miss: 0, reset: 0, pending: 0 };
if (options.reset) {
nextResetTime = options.reset.firstReset || Date.now() + options.reset.interval;
}
this.wrap = function(fn, thisobj, skipArgs) {
var stats = this.stats;
var fname = (fn.name || '_') + anonFnId++;
var cachedfunc;
var pending = this.pending;
log('wrapping function %s', fname);
cachedfunc = function() {
var self = thisobj || this;
var args = Array.prototype.slice.apply(arguments);
var lastArg = args[args.length - 1];
var callback;
var usePromise = typeof lastArg !== 'function';
var key, keyArgs;
if (usePromise) {
return new Promise(function(resolve, reject) {
args.push(function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
cachedfunc.apply(self, args);
});
}
callback = args.pop();
if (nextResetTime && nextResetTime < Date.now()) {
log('resetting cache %d', nextResetTime);
store.reset();
stats.reset++;
nextResetTime += options.reset.interval;
}
keyArgs = filterArgs(args, skipArgs);
key = keygen(fname, keyArgs);
log('fetching from cache %s', key);
store.get(key, onget);
function onget(err, data) {
var v;
if (!err && data !== undefined) {
log('cache hit %s', key);
process.nextTick(function() {
callback.call(self, err, data);
});
stats.hit++;
return;
}
log('cache miss %s', key);
if (pending) {
v = pending[key];
if (v === undefined) {
pending[key] = [];
stats.pending++;
} else {
log('fetch pending, queuing %s', key);
return v.push(callback);
}
}
args.push(function(err, res) {
if (!err) {
log('saving key %s', key);
store.set(key, res);
}
callback.call(self, err, res);
if (pending) {
v = pending[key];
if (v !== undefined && v.length) {
log('processing queue for %s', key);
process.nextTick(function() {
v.forEach(function(x) { x.call(self, err, res); });
});
stats.pending--;
delete pending[key];
}
}
});
fn.apply(self, args);
stats.miss++;
}
};
log('created cache function %s', fname);
cachedfunc.cacheName = fname;
cachedfunc.skipArgs = skipArgs;
return cachedfunc;
};
this.warmup = function() {
var args = Array.prototype.slice.apply(arguments);
var func = args.shift();
var res = args.pop();
validateCacheFunction(func);
var keyArgs = filterArgs(args, func.skipArgs);
var key = keygen(func.cacheName, keyArgs);
log('warming cache %s key %s', func.cacheName, key);
store.set(key, res);
};
this.invalidate = function() {
var args = Array.prototype.slice.apply(arguments);
var func = args.shift();
validateCacheFunction(func);
var keyArgs = filterArgs(args, func.skipArgs);
var key = keygen(func.cacheName, keyArgs);
log('invalidating %s key %s', func.cacheName, key);
store.expire(key);
};
this.isReady = function() {
return store.isReady();
};
},
debug: require('./debug')
};
module.exports = cache;