-
Notifications
You must be signed in to change notification settings - Fork 2
/
redisAdaptor.js
54 lines (44 loc) · 1.18 KB
/
redisAdaptor.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
var redisAdaptor = function (config) {
"use strict";
var redis = config.connection;
var client;
if (process.env.REDIS_URL) {
var url = require('url');
var redisURL = url.parse(process.env.REDIS_URL);
client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);
}
else {
client = redis.createClient();
}
return {
create: function(clap, callback) {
client.hmset(clap.time, clap, function(err){
callback(clap);
});
},
read: function(callback) {
var clapsLoad = [];
var len;
var cb = function(err, data) {
clapsLoad.push(data);
if(clapsLoad.length === len) {
callback(clapsLoad);
}
};
client.scan(0, function(err, data) {
var claps = data[1];
len = claps.length;
for(var i = 0; i < len; i++) {
client.hgetall(claps[i], cb);
}
});
},
delete: function(time, callback) {
client.del(time, function(err, reply) {
callback(reply);
});
}
};
};
module.exports = redisAdaptor;