forked from cubejs/cluster-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (144 loc) · 4.55 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
'use strict';
var _ = require('underscore'),
when = require('when'),
timeout = require('when/timeout'),
pipeline = require('when/pipeline');
var Cache = exports.Cache = function(namespace, usrAndMetaPromise){
_.extend(this, {
'namespace': namespace,
'getUsrAndMeta': function(){
return usrAndMetaPromise;
},
'pipeKeys': function(usrAndMeta){
return usrAndMeta.usr.keys(namespace);
},
'getPipeGet': function(key, loader, options){
return function(usrAndMeta){
return usrAndMeta.usr.get(namespace, key, loader, options);
};
},
'getPipeSet': function(key, value, options){
return function(usrAndMeta){
return usrAndMeta.usr.set(namespace, key, value, options);
};
},
'getPipeDel': function(key, options){
return function(usrAndMeta){
return usrAndMeta.usr.del(namespace, key, value, usrAndMeta.meta);
};
},
'getPipeWatch': function(key, onChange){
return function(usrAndMeta){
return usrAndMeta.usr.watch(namespace, key, onChange);
};
},
'getPipeUnwatch': function(key, onChange){
return function(usrAndMeta){
return usrAndMeta.usr.unwatch(namespace, key, onChange)
};
},
'pipeStat': function(usrAndMeta){
return usrAndMeta.usr.stat(namespace);
},
'pipeDestroy': function(usrAndMeta){
return usrAndMeta.usr.del('', namespace);
}
});
};
Cache.prototype.meta = function(){
return this.getUsrAndMeta().then(function(usrAndMeta){
return usrAndMeta.meta;
});
}
Cache.prototype.keys = function keys(){
return pipeline([this.getUsrAndMeta, this.pipeKeys]);
};
Cache.prototype.get = function get(key, loader, options){
return pipeline([this.getUsrAndMeta, this.getPipeGet(key, loader, options)]);
};
Cache.prototype.set = function set(key, value, options){
return pipeline([this.getUsrAndMeta, this.getPipeSet(key, value, options)]);
};
Cache.prototype.del = function del(key, options){
return pipeline([this.getUsrAndMeta, this.getPipeDel(key, options)]);
};
Cache.prototype.watch = function watch(key, onChange){
return pipeline([this.getUsrAndMeta, this.getPipeWatch(key, onChange)]);
};
Cache.prototype.unwatch = function unwatch(key, onChange){
return pipeline([this.getUsrAndMeta, this.getPipeUnwatch(key, onChange)]);
};
Cache.prototype.stat = function stat(){
return pipeline([this.getUsrAndMeta, this.pipeStat]);
};
Cache.prototype.destroy = function destroy(){
return pipeline([this.getUsrAndMeta, this.pipeDestroy]);
};
module.exports = {
get logger(){
if(!_.isFunction(process.getLogger)){
return {
'info': _.bind(console.log, console),
'debug': _.bind(console.log, console)
};
}
return process.getLogger(__filename);
},
'enable': _.once(function(options, emitter){
options = options || {};
emitter = emitter || require('cluster-emitter');
emitter = require('./lib/utils').decorateEmitter(emitter);
emitter.once('CLUSTER-ENABLE-CACHE', function(options){
var master = options.master,
mode = options.mode || 'master';
if(!master || mode === 'master'){
var mgr = require('./lib/cache-mgr'),
svr = mgr.createServer(mgr.app);
svr.listen(mgr.port, mgr.afterServerStarted);
}
else{
//it's master's job to fork another worker specificly as the cache manager
master.fork(master.options, {
'CACHE_MANAGER': true
});
}
});
//if it's none cluster mode, above registered cache initialization will take effect
//otherwise, in cluster mode, master will be responsible instead
emitter.to(['master']).emit('CLUSTER-ENABLE-CACHE', options);
}),
'use': function(namespace, options){
var _this = this,
logger = _this.logger,
actualOptions = options || {};
_.defaults(actualOptions, {
'persist': false,
'expire': 0,
'timeout': 3000,
'lastPersisted': -1
});
return new Cache(namespace, pipeline([
function(domain){
logger.debug('[cache] using domain:%s', domain);
return _this.user.use(domain);
},
function(usr){
logger.debug('[cache] usr ready, and using namespace:%s & options:%j', namespace, actualOptions);
return when.join(usr, usr.get('', namespace, function(){
return actualOptions;
}));
},
function(resolve){
var usr = resolve[0],
meta = resolve[1];
logger.debug('[cache] namespace:%s reserved with meta:%j', namespace, meta);
return {
'usr': usr,
'meta': meta
};
}
], actualOptions.domain));//optional
},
'manager': require('./lib/cache-mgr'),
'user': require('./lib/cache-usr')
};