NODEJS(15)Platform - memcached

本文介绍了一个Node.js客户端驱动程序node-memcached,并详细解释了如何使用它进行缓存操作,包括设置依赖项、定义Memcached实例及使用公共方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值