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
相关推荐
基于vue+nodejs毕业设计-在线购物商城系统(源码+数据库+文档说明)含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用,该项目可以作为毕设、期末大作业使用,...
基于vue+nodejs毕业设计-图书管理系统(源码+论文资料),该项目是个人毕设项目,答辩评审分达到98分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。该资源主要针对计算机、通信、人工...
cos-nodejs-sdk-v5腾讯云 COS Nodejs SDK()installnpm i cos-nodejs-sdk-v5 --savedemo// 引入模块var COS = require('cos-nodejs-sdk-v5');// 创建实例var cos = new COS({ SecretId: '...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-slide-1.1.5-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-which-1.0.5-7.el6.centos.alt.noarch.rpm
nodejs010-nodejs-rimraf-2.2.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-osenv-0.0.3-6.el6.centos.alt.noarch.rpm
nodejs010-nodejs-nan-0.4.4-2.2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-npmlog-0.0.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-promzard-0.2.0-7.el6.centos.alt.noarch.rpm
nodejs010-nodejs-sha-1.2.1-1.1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-semver-2.1.0-3.el6.centos.alt.noarch.rpm
nodejs010-nodejs-hoek-0.9.1-2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-once-1.3.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-request-2.25.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-npmconf-0.1.12-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-glob-3.2.8-1.el6.centos.alt.noarch.rpm