NODEJS(15)Platform - memcached

阿丰 2014-05-31

NODEJS(15)Platform - memcached

1. memcached Introdution
This is a nodeJS client driver, node-memcached.
It is using consistent hashing, oh, my old days, I use memcached when this algorithm is not implemented, I implemented it myself once.

It is using the ASCII protocol, oh, I used to heard that long time ago, it plans to use binary protocol. But till now, it is not binary, the good things for this is we can see what is send over the wire.

There are punch of options to define a Memcached Instance.
maxKeySize, maxExpiration, maxValue, poolSize, algorithm, reconnect, timeout, retries, failures, retry, failOverServers,
keyCompression, idle

Define the Memcached with option

var memcached = new Memcached(‘localhost:11211’, {retries:10, retry:10000, remove:true, failOverServers:[“localhost:11212”]});

Public Methods
memcached.touch(key,lifetime,callback)  lifetime is measured by seconds
memcached.get(key, callback)
memcached.get(‘foo’, function(err, data){
     console.log(data);
});
memcached.getMulti(keys, callback)
memcached.getMulti([‘foo’,’bar’], function(err, data){
     console.log(data.foo + data.bar);
});

memcached.set(key, value, lifetime, callback)
memcached.set(‘foo’,’bar’,10,function(err){ });

memcached.replace(key, value, lifetime, callback)
memcached.replace(‘foo’, ‘bar’, 10, function(err){ });

memcached.del(key, callback)
memcached.end

2. How I Use It
Add the dependencies
{
     "name": "buglist",
     "version": "0.0.1",
     "private": true,
     "dependencies": {
          "express": "4.2.0",
          "body-parser": "1.2.0",
          "generic-pool": "2.0.4",
          "mongodb": "1.4.5",
          "log4js": "0.6.14",
          "memcached": "0.2.8"
     }
}

Using Memcached
var Memcached = require('memcached');
var memcached = new Memcached({'127.0.0.1:11211': 1, '127.0.0.1:11212': 1 })

             memcached.get("bugs", function(err, data){
                  if(data != false){
                       logger.debug("hitting the memached =" + data + "!");
                       res.json(data);
                  }else{
                       logger.debug("missing the memached!");
                       db.collection('bugs').find({}).toArray(function(err, bugs){
                              if (err) res.json(500, err);
                              else {
                                   logger.debug("setting the memached!");
                                   memcached.set("bugs", bugs ,120,function(err){
                                        if(err != undefined) res.json(500, err);
                                   });
                                   res.json(bugs);
                              }
                              pool.release(db);
                         });
                  }
             });

Here is the log, first miss, second hit
[2014-05-30 17:26:15,776] [DEBUG] [M] missing the memached!

[2014-05-30 17:26:15,778] [DEBUG] [M] setting the memached!

[2014-05-30 17:26:15,780] [INFO ] [M] 127.0.0.1 - - "GET /bugs HTTP/1.1" 200 130 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"

[2014-05-30 17:26:22,356] [DEBUG] [M] hitting the memached =[object Object]! [2014-05-30 17:26:22,358] [INFO ] [M] 127.0.0.1 - - "GET /bugs HTTP/1.1" 200 130 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"


References:
https://github.com/3rd-Eden/node-memcached

Graceful closing and Process
http://heyrod.com/snippet/s/node-js-process-on-sigint.html
http://nodejs.org/api/process.html

Nodejs request package
https://github.com/mikeal/request

相关推荐