-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredis-util.js
43 lines (38 loc) · 1.04 KB
/
redis-util.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
module.exports.authClientCreator = function(redisUrl) {
return function() {
var url = require("url").parse(redisUrl);
var client = require("redis").createClient(url.port, url.hostname);
client.auth(url.auth.split(":")[1]);
return client;
}
};
module.exports.setup = function(redisConnect) {
var redisOnline = false;
var client = redisConnect();
client.on('connect', function() {
console.log("Something AWESOME happened: WE HAZ REDIS!");
redisOnline = true;
});
client.on('error', function(e) {
console.log("Something unawesome happened: " + e.message);
redisOnline = false;
});
process.on("exit", client.quit);
client.errorResponse = function(_, res, next) {
if (redisOnline) {
next();
} else {
res.send("Something unawesome happened. (Redis is temporarily unavailable.)", 500);
}
}
return client;
}
module.exports.namespaced = function(namespace) {
return function(key) {
if (namespace) {
return namespace + ":" + key;
} else {
return key;
}
}
};