forked from grahamking/Key-Value-Polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemg.js
56 lines (37 loc) · 1.3 KB
/
memg.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
var net = require('net');
/* Create the cache object */
var CACHE = {};
var SINGLE = (process.argv[2] === '--single') ? true : false;
/* Create an async network wrapper */
var server = net.createServer(function(sock) {
"use strict";
/* Handle the connection received on our network socket */
sock.on('data', function (chunk) {
/* Upon receiving the socket connection, extract the data */
var data = chunk.toString();
/* Start splitting the command string */
var parts = data.split('\r\n'), tmp = parts[0].split(' ');
var cmd = tmp[0], key = tmp[1], val;
var msg = [];
/* Take action according to the command */
if (cmd === 'get') {
val = CACHE[key];
if (val) {
msg.push("VALUE ", key, " 0 ", val.length, "\r\n")
msg.push(val, "\r\n")
};
msg.push("END\r\n");
} else if (cmd === 'set') {
val = parts.slice(1, -1);
var length = +tmp[4];
if (val) {
CACHE[key] = val.join("\r\n").slice(0, length);
msg.push("STORED\r\n");
};
};
/* Write out the response */
sock.write(msg.join(""));
});
if (SINGLE) process.exit(1);
});
server.listen(11211);