-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeMemcacheClient.js
More file actions
335 lines (267 loc) · 12.1 KB
/
nodeMemcacheClient.js
File metadata and controls
335 lines (267 loc) · 12.1 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
var net = require('net');
var ns = require('./nodeSocket');
const DEFAULT_MC_HOST = '127.0.0.1';
const DEFAULT_MC_PORT = 11211;
//public functions
function MemcacheClient(server, port) {
if(!server || server === undefined){
server = DEFAULT_MC_HOST;
}
if(!port || port === undefined){
port = DEFAULT_MC_PORT;
}
this.server = server;
this.port = port;
}
//retrieve commands
//Provide an input of array of keys to fetch along w/ callback to call after fetch is complete
MemcacheClient.prototype.get = function(keys, options, callback) {
var objToHandleRequest = {}
this.defaultSetup(objToHandleRequest, options, callback);
objToHandleRequest.connect = this.makeGetRequest.bind(this); //callback function to call by the socket layer once we've gotten the connection few lines down
objToHandleRequest.keys = keys;
objToHandleRequest.response = []; //result that will be given out
objToHandleRequest.state = READING_META_DATA; //state of parsing the response
ns.getConnection(this.server, this.port, objToHandleRequest); //get the connection (from the pool or a new one) and then callback into the .connect fxn
}
//set commands
MemcacheClient.prototype.set = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makeSetRequest.bind(this);
this.setDefaultSetup(keyObj, objToHandleRequest, options, callback);
}
MemcacheClient.prototype.add = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makeAddRequest.bind(this);
this.setDefaultSetup(keyObj, objToHandleRequest, options, callback);
}
MemcacheClient.prototype.replace = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makeReplaceRequest.bind(this);
this.setDefaultSetup(keyObj, objToHandleRequest, options, callback);
}
MemcacheClient.prototype.append = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makeAppendRequest.bind(this);
this.setDefaultSetup(keyObj, objToHandleRequest, options, callback);
}
MemcacheClient.prototype.prepend = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makePrependRequest.bind(this);
this.setDefaultSetup(keyObj, objToHandleRequest, options, callback);
}
MemcacheClient.prototype.del = function(keyObj, options, callback) {
var objToHandleRequest = {}
objToHandleRequest.connect = this.makeDeleteRequest.bind(this);
this.defaultSetup(objToHandleRequest, options, callback);
objToHandleRequest.keyObj = keyObj;
if(!(keyObj.noreply !== undefined && !keyObj.noreply)) {
objToHandleRequest.state = READING_META_DATA;
}
ns.getConnection(this.server, this.port, objToHandleRequest);
}
//private functions
MemcacheClient.prototype.makeGetRequest = function(error, client, objToHandleRequest) {
if(error) {
console.log(error);
if(objToHandleRequest) {
objToHandleRequest.user_callback(error);
}
return;
}
objToHandleRequest.client = client;
//write the request for get
var request = 'get ' + objToHandleRequest.keys.join(' ') + '\r\n';
client.write(request, 'binary', function(){});
}
const READING_META_DATA = 1;
const READING_VALUE = 2;
MemcacheClient.prototype.parseResponse = function(error, data, objToHandleRequest) {
if(error) {
console.log(error);
if(objToHandleRequest) {
objToHandleRequest.user_callback(error);
}
return;
}
var dataString = data.toString('binary');
objToHandleRequest.complete_response += dataString;
while(objToHandleRequest.complete_response.length) {
if(objToHandleRequest.state === READING_META_DATA){
var endOfMetaData = objToHandleRequest.complete_response.indexOf('\r\n');
if(endOfMetaData != -1) {
var response_line = objToHandleRequest.complete_response.substr(0, endOfMetaData);
var meta_data_split = response_line.split(' ');
//found VALUE
if(meta_data_split[0] === "VALUE" && meta_data_split.length == 4) {
var result = {};
result.key = meta_data_split[1];
result.flag = parseInt(meta_data_split[2]);
result.length = parseInt(meta_data_split[3]);
objToHandleRequest.response.push(result);
objToHandleRequest.complete_response = objToHandleRequest.complete_response.substr(endOfMetaData+2/*the \r\n at the end of the meta data section*/); //advance past the meta data section
objToHandleRequest.state = READING_VALUE;
}
else if(meta_data_split[0] === "END") {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
objToHandleRequest.user_callback(null, objToHandleRequest.response);
return;
}
else if(meta_data_split[0] === "STORED") {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
objToHandleRequest.user_callback(null);
return;
}
else if(meta_data_split[0] === "DELETED") {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
objToHandleRequest.user_callback(null);
return;
}
else {
//see if there were any errors
var reExp = /ERROR/i;
if(reExp.test(objToHandleRequest.complete_response)){
ns.releaseConnection(objToHandleRequest.client, false);
}
else {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
}
objToHandleRequest.user_callback(new Error('error on response: with response = ' + objToHandleRequest.complete_response));
return;
}
}
else {
return;
}
}
if(objToHandleRequest.state === READING_VALUE){
if(!objToHandleRequest.response.length) {
return;
}
var resultObj = objToHandleRequest.response[objToHandleRequest.response.length-1]; //pick the last element on the array - cause thats the one thats getting filled up
if(objToHandleRequest.complete_response.length < (resultObj.length + 2 /*for \r\n*/)) { //more to read
return;
}
resultObj.value = objToHandleRequest.complete_response.substr(0, resultObj.length);
var amtToRemove = resultObj.length + 2/*include the \r\n after the response*/;
objToHandleRequest.complete_response = objToHandleRequest.complete_response.substr(amtToRemove);
objToHandleRequest.state = READING_META_DATA;
}
}
}
MemcacheClient.prototype.setDefaultSetup = function(keyObj, objToHandleRequest, options, callback) {
this.defaultSetup(objToHandleRequest, options, callback);
objToHandleRequest.keyObj = keyObj;
if(!(keyObj.noreply !== undefined && !keyObj.noreply)) {
objToHandleRequest.state = READING_META_DATA;
}
ns.getConnection(this.server, this.port, objToHandleRequest);
}
const DELETE_COMMAND = 'delete';
MemcacheClient.prototype.makeDeleteRequest = function(error, client, objToHandleRequest, command) {
if(error) {
if(objToHandleRequest) {
objToHandleRequest.user_callback(error);
}
return null;
}
var keyObj = objToHandleRequest.keyObj;
//console.log('request = ' + request);
objToHandleRequest.client = client;
var request = DELETE_COMMAND + ' ' +
keyObj.key + ' ' +
(keyObj.noreply === undefined || !keyObj.noreply ? '' : ' noreply') +
'\r\n' ;
client.write(request, 'binary', function(){
//since we're not going to get a DELETED response here - we should release the connection back to the pool
if(keyObj.noreply !== undefined && keyObj.noreply) {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
objToHandleRequest.user_callback(null);
}
});
return;
}
MemcacheClient.prototype.fillSetTypeRequests = function(error, client, objToHandleRequest, command) {
if(error) {
if(objToHandleRequest) {
objToHandleRequest.user_callback(error);
}
return null;
}
var keyObj = objToHandleRequest.keyObj;
//console.log('request = ' + request);
objToHandleRequest.client = client;
var request = command + ' ' +
keyObj.key + ' ' +
keyObj.flag + ' ' +
keyObj.expires + ' ' +
keyObj.value.length +
(keyObj.noreply === undefined || !keyObj.noreply ? '' : ' noreply') +
'\r\n' +
keyObj.value +
'\r\n';
client.write(request, 'binary', function(){
//since we're not going to get a STORED response here - we should release the connection back to the pool
if(keyObj.noreply !== undefined && keyObj.noreply) {
ns.releaseConnection(objToHandleRequest.client, objToHandleRequest.putConnectionBackIntoPool);
objToHandleRequest.user_callback(null);
}
});
return;
}
const SET_COMMAND = 'set';
MemcacheClient.prototype.makeSetRequest = function(error, client, objToHandleRequest) {
return this.fillSetTypeRequests(error, client, objToHandleRequest, SET_COMMAND);
}
const ADD_COMMAND = 'add';
MemcacheClient.prototype.makeAddRequest = function(error, client, objToHandleRequest) {
return this.fillSetTypeRequests(error, client, objToHandleRequest, ADD_COMMAND);
}
const REPLACE_COMMAND = 'replace';
MemcacheClient.prototype.makeReplaceRequest = function(error, client, objToHandleRequest) {
return this.fillSetTypeRequests(error, client, objToHandleRequest, REPLACE_COMMAND);
}
const APPEND_COMMAND = 'append';
MemcacheClient.prototype.makeAppendRequest = function(error, client, objToHandleRequest) {
return this.fillSetTypeRequests(error, client, objToHandleRequest, APPEND_COMMAND);
}
const PREPEND_COMMAND = 'prepend'
MemcacheClient.prototype.makePrependRequest = function(error, client, objToHandleRequest) {
return this.fillSetTypeRequests(error, client, objToHandleRequest, PREPEND_COMMAND);
}
MemcacheClient.prototype.close = function(objToHandleRequest) {
if(objToHandleRequest && objToHandleRequest.user_callback) {
objToHandleRequest.user_callback(new Error("Server closed"));
}
}
MemcacheClient.prototype.timeout = function(objToHandleRequest) {
if(objToHandleRequest && objToHandleRequest.user_callback) {
objToHandleRequest.user_callback(new Error("Server timedout"));
}
}
MemcacheClient.prototype.error = function(error, objToHandleRequest) {
if(objToHandleRequest && objToHandleRequest.user_callback) {
objToHandleRequest.user_callback(error);
}
}
MemcacheClient.prototype.defaultSetup = function(objToHandleRequest, options, callback) {
objToHandleRequest.close = this.close.bind(this);
objToHandleRequest.timeout = this.timeout.bind(this);
objToHandleRequest.error = this.error.bind(this);
if(options && options.putConnectionBackIntoPool !== undefined) {
objToHandleRequest.putConnectionBackIntoPool = options.putConnectionBackIntoPool;
}
else {
objToHandleRequest.putConnectionBackIntoPool = true;
}
if(options && options.timeout !== undefined) {
objToHandleRequest.timeout = options.timeout;
}
else {
objToHandleRequest.timeout = -1;
}
objToHandleRequest.user_callback = callback;
objToHandleRequest.complete_response = '';
objToHandleRequest.read = this.parseResponse.bind(this);
}
module.exports = MemcacheClient;