Sample programs that use Nodejs how to read memcache

Source: Internet
Author: User
Tags require wrapper

  I mainly introduced the use of Nodejs how to read the Memcache sample program, we refer to the use of the bar

The code is as follows: var memcache = require (' memcache ')    , HTTP = require (' http ')    , url = require (' URL ') &nbs P  , QS = require (' querystring ')    , memsettings = {port:2000, host: ' 10.6.0.6 '}    , Httpse Ttings = {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.conn ected = false; });   function GetData (key, wrapper, Res) {    client.get (key, function (error, result) {      &N Bsp    //All of the callbacks have two arguments.            //' result ' may contain things which aren ' t great, but       &NBS P   &nbsp Aren ' t really errors, like ' not_stored '             if (!error) {      &NBS P         if (result) {                    cacheobject[ Key] = result;                     Res.write (wrapper + ' (' + result + ') ');                } else {                & nbsp   Res.write (' no data ');                                 Res.end ( );             {     };    /*if (Cacheobject[key]) {        Response.Write (Cacheobject[key);         Response.End ();    } else  {        client.get (key, function (error, result) {      &NB Sp     //All of the callbacks have two arguments.            //' result ' may contain things which aren ' t great, but       &NBS P    //aren ' t really errors, like ' not_stored '             if (!error) {  &nbs P             Cacheobject[key] = result;                 cacheobject[key][' timeoutid ' = settimeout (function () {  &nbs P                 client.get (key, function (error, result) {      &NBS P                 if (!error) {              &NBSP ;             Cacheobject[key] = result;                                   & nbsp        });                });                 Response.Write (result);                 Response.End ();            }        }    }*/}   Httpserver = Http.createserver (function (req, res) {    var urlobj, Queryobj;   & nbsp Urlobj = Url.parse (Req.url);     Res.writehead, {"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 ();   &N Bsp }     *     switch (urlobj.pathname) {        case '/pai ':       &N Bsp     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);  

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.