-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.js
More file actions
184 lines (158 loc) · 4.2 KB
/
redis.js
File metadata and controls
184 lines (158 loc) · 4.2 KB
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
'use strict';
var redis = require('redis');
var debug = require('debug')('obcachejs');
var redisStore = {
init: function(options) {
var client;
var prefix;
var keylen = 0;
var maxAge = (options && options.maxAge) || 60000;
var url = options.redis.url;
var host = options.redis.host || 'localhost';
var port = options.redis.port || 6379;
if (!options || isNaN(Number(options.id))) {
throw new Error('redis requires numeric id option, got: ' + options.id);
}
var clientOptions = {};
if (url) {
clientOptions.url = url;
} else {
clientOptions.socket = {
host: host,
port: port,
connectTimeout: options.redis.connectTimeout || 5000
};
}
if (options.redis.database !== undefined) {
clientOptions.database = options.redis.database;
} else if (!options.redis.twemproxy) {
clientOptions.database = options.id;
}
client = redis.createClient(clientOptions);
var connected = false;
var pendingOps = [];
function flushPendingOps() {
var ops = pendingOps;
pendingOps = [];
ops.forEach(function(op) { op(); });
}
function whenReady(op) {
if (connected) {
op();
} else {
pendingOps.push(op);
}
}
client.on('error', function(err) {
debug('redis error ' + err);
connected = false;
});
client.connect().then(function() {
debug('redis connected');
connected = true;
flushPendingOps();
if (!options.redis.twemproxy) {
client.dbSize().then(function(size) {
keylen = size;
}).catch(function(err) {
debug('dbsize error ' + err);
});
}
}).catch(function(err) {
debug('redis connect error ' + err);
connected = false;
// Fail pending operations
var ops = pendingOps;
pendingOps = [];
ops.forEach(function(op) { op(err); });
});
if (options.redis.twemproxy) {
debug('twemproxy compat mode. stats on keys will not be available.');
}
prefix = 'obc:' + options.id + ':';
var rcache = {
maxAge: maxAge,
client: client,
get: function(key, cb) {
var k = prefix + key;
whenReady(function(err) {
if (err) return cb(err);
client.get(k).then(function(data) {
var result;
if (!data) {
return cb(null);
}
try {
result = JSON.parse(data);
} catch (e) {
return cb(e);
}
return cb(null, result);
}).catch(function(err) {
cb(err);
});
});
},
set: function(key, val, cb) {
var k = prefix + key;
var ttl = Math.floor(this.maxAge / 1000);
var obj;
try {
obj = JSON.stringify(val);
} catch (err) {
if (cb) cb(err);
return;
}
whenReady(function(err) {
if (err) {
if (cb) cb(err);
return;
}
debug('setting key ' + k + ' in redis with ttl ' + ttl);
client.setEx(k, ttl, obj).then(function() {
if (cb) cb(null, val);
}).catch(function(err) {
if (cb) cb(err);
});
});
},
expire: function(key, cb) {
var k = prefix + key;
whenReady(function(err) {
if (err) {
cb && cb(err);
return;
}
client.expire(k, 0).then(function() {
cb && cb(null);
}).catch(function(err) {
cb && cb(err);
});
});
},
reset: function() {
if (options.redis.twemproxy) {
throw new Error('Reset is not possible in twemproxy compat mode');
}
return client.flushDb().catch(function(err) {
debug('flushdb error ' + err);
throw err;
});
},
size: function() {
return 0;
},
keycount: function() {
if (options.redis.twemproxy) {
return -1;
}
return keylen;
},
isReady: function() {
return connected;
}
};
return rcache;
}
};
module.exports = redisStore;