Created
November 12, 2010 03:54
-
-
Save anonymous/673704 to your computer and use it in GitHub Desktop.
Connecting to a memcached server via node.js
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 http = require('http') | |
, sys = require('sys') | |
, url = require('url') | |
, path = require('path') | |
, fs = require('fs') | |
, net = require('net'); | |
var crlf = "\r\n"; | |
var crlf_len = crlf.length; | |
var mc_server = { | |
ms: '' | |
, buffer: '' | |
, conn: function (port, host, callback) { | |
if (!this.conn) { | |
this.ms = new net.createConnection(port, host); | |
mc_server.ms.on('connect', function () { | |
sys.puts('Connected to server: localhost:' + port); | |
}); | |
mc_server.ms.on('error', function (exception) { | |
sys.puts('Error: ' + exception); | |
}); | |
mc_server.ms.on('data', function (data) { | |
mc_server.buffer += data; | |
sys.puts(data); | |
}); | |
mc_server.ms.on('end', function () { | |
if (mc_server.ms && mc_server.ms.readyState) { | |
mc_server.ms = null; | |
sys.puts('Disconnected!'); | |
} | |
}); | |
} | |
callback(this.ms); | |
} | |
, query: function (stmt) { | |
this.conn(11211, 'localhost', function (ms) { | |
//sys.puts(sys.inspect(ms)); | |
ms.write(stmt); | |
}); | |
} | |
}; | |
mc_server.query('get json_foo ' + crlf); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment