-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
258 lines (219 loc) · 7.02 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
'use strict';
// nosql - static API for RAM, SSC, or Redis key-value store
if (process.env.NOSQL_COVERAGE) require('blanket');
function NoSQL (collection, options, done) {
this.collection = collection || 'default';
if (!options) options = {};
// ram, ssc (strong store cluster), redis
this.store = options.store || 'ram';
this.default_cb = options.done || function (ignore) {};
this.expire = 10 * 60; // convert minutes to seconds
this.ramCache = {};
this.cfg = {};
if (options.expire !== undefined) {
this.expire = parseFloat(options.expire) * 60; // min to sec
}
switch (this.store) {
case 'ram':
if (this.expire) {
this._interval = setInterval(this.reset, this.expire * 1000);
}
if (done) done();
break;
case 'ssc': // Strong-Store-Cluster
try {
this.ssc = require('strong-store-cluster')
.collection(this.collection);
if (this.expire) {
this.ssc.configure({ expireKeys: this.expire });
}
if (done) done(null, 1);
}
catch (e) {
console.error(e);
console.log('cannot load strong-store-cluster' +
' see README to understand consequences');
this.store = 'ram';
if (done) done('cannot load strong-store-cluster');
}
break;
case 'redis':
this.cfg.redis = options.redis || {
host: 'localhost',
port: 6379,
dbid: 0,
};
if (this.expire) {
this._interval = setInterval(this.reset, this.expire * 1000);
}
this.redis_connect(done || this.default_cb);
break;
}
}
NoSQL.prototype.get = function (key, done) {
if (!done) done = this.default_cb;
switch (this.store) {
case 'ram':
done(null, this.ramCache[key]);
break;
case 'ssc':
this.ssc.acquire(key, function (err, keylock, value) {
if (err) { console.error(err); }
keylock.release();
done(err, value);
});
break;
case 'redis':
this.redis.hget(this.collection, key, done);
break;
}
};
NoSQL.prototype.set = function (key, val, done) {
if (!done) done = this.default_cb;
switch (this.store) {
case 'ram': {
// mimic redis result
const was_set = this.ramCache[key] ? 0 : 1;
this.ramCache[key] = val;
done(null, was_set);
break;
}
case 'ssc': {
// SSC saves JSON serialized, so put val into an object
const newVal = val;
this.ssc.acquire(key, function (err, keylock, oldVal) {
if (err) { console.error(err); }
keylock.set(newVal);
keylock.release();
done(err, oldVal ? 0 : 1);
});
break;
}
case 'redis':
this.redis.hset(this.collection, key, val, done);
break;
}
};
NoSQL.prototype.del = function (key, done) {
if (!done) done = this.default_cb;
switch (this.store) {
case 'ram':
delete this.ramCache[key];
done(null, 1);
break;
case 'ssc':
this.ssc.acquire(key, function (err, keylock, val) {
if (err) { console.error(err); }
keylock.del();
keylock.release();
done(err, 1);
});
break;
case 'redis':
this.redis.hdel(this.collection, key, done);
break;
}
};
NoSQL.prototype.incrby = function (key, incr, done) {
if (!done) done = this.default_cb;
incr = parseFloat(incr);
switch (this.store) {
case 'ram': {
if (isNaN(incr)) incr = 1;
let val = parseFloat(this.ramCache[key]) || 0;
if (isNaN(val)) val = 0;
this.ramCache[key] = parseFloat(val) + incr;
done(null, this.ramCache[key]);
break;
}
case 'ssc':
if (isNaN(incr)) incr = 1;
this.ssc.acquire(key, function (err, keylock, oldVal) {
if (err) { console.error(err); }
if (!oldVal) {
oldVal = 0;
}
else {
oldVal = parseFloat(oldVal);
if (isNaN(oldVal)) oldVal = 0;
}
keylock.set(oldVal + incr);
keylock.release();
done(err, oldVal + incr);
});
break;
case 'redis':
this.redis.hincrby(this.collection, key, incr, done);
break;
}
};
NoSQL.prototype.reset = function (done) {
if (!done) done = this.default_cb;
console.log(`clearing ${ this.store}`);
switch (this.store) {
case 'ram':
this.ramCache = {};
done(null, 1);
break;
case 'ssc':
// Strong Store Cluster has no such option
done(null, 1);
break;
case 'redis':
this.redis.del(this.collection, done);
break;
}
};
// Redis DB functions
NoSQL.prototype.redis_connect = function (done) {
const nosql = this;
let ranDone = 0;
if (nosql.redis && nosql.redis_pings) {
console.log('redis already connected');
if (done) { ranDone++; done(null, true); }
return;
}
const redis = require('redis'); // npm module
nosql.redis = redis.createClient(
nosql.cfg.redis.port || 6379,
nosql.cfg.redis.host || 'localhost'
);
nosql.redis.on('error', function (error) {
// console.log('nosql redis error: ' + error.message);
if (done && !ranDone) { ranDone++; done(error); }
});
nosql.redis.on('connect', function () {
// console.log('redis connected');
if (nosql.cfg.redis.dbid) {
console.log(`redis db ${ nosql.cfg.redis.dbid } selected`);
nosql.redis.select(nosql.cfg.redis.dbid);
}
ranDone++;
nosql.redis_ping(done);
});
};
NoSQL.prototype.redis_ping = function (done) {
const nosql = this;
const nope = function (err) {
nosql.redis_pings=false;
done(err, false);
};
if (!nosql.redis) { return nope('no redis!'); }
nosql.redis.ping(function (err, res) {
if (err ) { return nope(err); }
if (res !== 'PONG') { return nope('invalid redis response'); }
nosql.redis_pings=true;
done(null, true);
});
};
NoSQL.prototype.shutdown = function () {
if (this.redis) {
// console.log('disconnecting redis')
this.redis.quit();
}
if (this._interval) {
// console.log('clearing _interval')
clearInterval(this._interval)
}
}
module.exports = NoSQL;