-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.js
74 lines (64 loc) · 2.27 KB
/
redis.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
// redis functions
var client = require('redis').createClient(process.env.REDIS_URL);
var redisMeow = (function() {
function postMeow(postObj, callback) {
client.incr('postcounter', function(err, reply) {
if (err) {
// We created this error handler to find out why redis was hanging.
// However it is incredibly difficult to test this, so we've commented
// it out.
//console.log("ERROR +++++++", err);
}
var thisPostIndex = reply;
var redisPostObj = JSON.parse(postObj);
// NEW ADDITIONS
redisPostObj.key = thisPostIndex;
client.lpush(['keys', thisPostIndex], function(err, reply) {
console.log('reply from lpush ..... = ......' + reply);
});
client.hmset(thisPostIndex, redisPostObj, function() {
callback();
});
});
}
function getMeow(callback) {
client.get('postcounter', function(err, reply) {
//var i = reply;
var multi = client.multi();
var keysToGet = [];
client.lrange('keys', 0, 6, function(err, reply) {
keysToGet = reply;
console.log('keysToGet =========', keysToGet);
keysToGet.forEach(function(thisKey) {
console.log('this key', thisKey);
multi.hgetall(thisKey);
});
multi.exec(function(err, replies) {
console.log('multi output = ', replies);
callback(JSON.stringify(replies));
});
});
});
}
function deleteMeow(key, callback) {
console.log('KEY TO DELETE =', key);
client.lrem('keys', 0, key, function(err, reply) {
if (err) {
console.log(err);
} else {
console.log(reply);
client.del(key, function(error, reply) {
console.log(reply);
callback();
});
}
});
}
return {
postMeow: postMeow,
getMeow: getMeow,
deleteMeow: deleteMeow,
client: client
};
})();
module.exports = redisMeow;