Created
December 23, 2011 20:37
-
-
Save mmalecki/1515297 to your computer and use it in GitHub Desktop.
Messy memcached partial reimplementation I wrote on my very first day with node. Last time I checked, it's faster in some cases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var store = { } | |
var server = net.createServer(function (socket) { | |
var buffer = ""; | |
var index = 0; | |
socket.on("data", function(data) { | |
buffer += data.toString('utf8'); | |
index = buffer.indexOf("\n"); | |
if (index != -1) { | |
command = buffer.substr(0, index); | |
buffer = buffer.substr(index + 1); | |
} | |
console.log(buffer) | |
x = command.split(' '); | |
x[x.length - 1] = x[x.length - 1].trim() | |
if (x[0] == 'get') | |
socket.write(store[x[1].trim()]); | |
else if (x[0] == 'set') | |
store[x[1]] = x[2].trim(); | |
else if (x[0] == 'show') | |
console.log(store); | |
}); | |
}); | |
server.listen(1337, "127.0.0.1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment