Nodejs example of reading memcache _ javascript skills

Source: Internet
Author: User
I mainly introduced how to read the sample program of memcache using nodejs. The 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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.