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完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs-microservice-starter, NodeJS RESTful API Microservice Starter NodeJS RESTful API Microservice v1.2.0这个存储库包含运行 NodeJS rest式 API Microservice启动器的完整配置。 要求NodeJS用于本地开
DigitalOcean-Nodejs-Deployment--源码.rar
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs-file-downloader 是一个用于下载文件的简单实用程序。 它隐藏了处理流、重定向、路径和重复文件名的复杂性。 可以自动重复失败的下载。 如果您遇到任何错误或有疑问,请不要犹豫,打开一个问题。安装$ npm ...
nodejs-win-x64最新安装包,nodejs的官方网站下载的64位安装包,官网速度真的不敢恭维,所以发上来给大家
nodejs-socketio-chat源码,是一个很不错的Android代码,有兴趣的伙伴们抽时间可以看一下把。
nodejs-auth-master.rar
use nodejs+express to write api,use react to ajax,use webpack to package. github:https://github.com/zhaoyu69/nodejs-express-react 有问题欢迎指出,如果有用 欢迎star★。
nodejs小工具--pdf拆分
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs进程通信--pm2 cluster方式, pm2版本 2.4.2,node 版本 v7.5.0