Copy codeThe Code is as follows:
Var memcache = require ('memcache ')
, Http = require ('http ')
, Url = require ('url ')
, Qs = require ('querystring ')
, Memsettings = {port: 2000, host: '10. 6.0.6 '}
, Httpsettings = {port: 3000}
, CacheObject = {}
, Httpserver
, Client
, RequestHandler;
Client = new memcache. Client (memsettings. port, memsettings. host );
//...
Client. on ('connect ', function (){
Client. connected = true;
});
Client. on ('close', function (){
// No arguments-connection has been closed
Client. connected = false;
});
Function getData (key, wrapper, res ){
Client. get (key, function (error, result ){
// All of the callbacks have two arguments.
// 'Result' may contain in things which aren't great,
// Aren't really errors, like 'not _ stored'
If (! Error ){
If (result ){
CacheObject [key] = result;
Res. write (wrapper + '(' + result + ')');
} Else {
Res. write ('no data ');
}
Res. end ();
}
});
/* If (cacheObject [key]) {
Response. write (cacheObject [key]);
Response. end ();
} Else {
Client. get (key, function (error, result ){
// All of the callbacks have two arguments.
// 'Result' may contain in things which aren't great,
// Aren't really errors, like 'not _ stored'
If (! Error ){
CacheObject [key] = result;
CacheObject [key] ['timeotid'] = setTimeout (function (){
Client. get (key, function (error, result ){
If (! Error ){
CacheObject [key] = result;
}
});
});
Response. write (result );
Response. end ();
}
});
}*/
}
Httpserver = http. createServer (function (req, res ){
Var urlObj, queryObj;
UrlObj = url. parse (req. url );
Res. writeHead (200, {"Content-Type": "text/javascript; charset = UTF-8 "});
QueryObj = qs. parse (urlObj. query );
Path = queryObj. pathname;
If (queryObj. gid ){
GetData (queryObj. gid, queryObj. callback | 'callback', res );
} Else {
Res. write ('no input ');
Res. end ();
}
/*
Switch (urlObj. pathname ){
Case '/pai ':
Res. write (urlObj. pathname );
Break;
Case '/qiang ':
Res. write (urlObj. pathname );
Break;
Default:
Res. write ('default ');
Break;
}
Res. end ();
*/
});
Client. connect ();
Httpserver. listen (httpsettings. port );
Console. log ('HTTP server listening on' + httpsettings. port );