node使用redis缓存
2014-12-14 20:51
274 查看
最近想知道node相关的缓存,就找到了redis
然后自己实现了node api数据的缓存
我先写了个模块,当做redis的链接对象的工厂
新建了redis_factory.js
var redis = require('redis');
var poolModule = require('generic-pool');//使用generic-pool池化
var pool = poolModule.Pool({
name : 'redisPool',
create : function(callback) {
var client = redis.createClient();
callback(null, client);
},
destroy : function(client) {
client.quit();
},
max : 100,
min : 5,
idleTimeoutMillis : 30000,
log : false
});
exports.pool = pool;
然后在api方法里面这样用
#include <iostream>
#include"stack.h"
#include"stack.cpp"
using namespace std;
int main()
{
Stack<double> stack;
stack.push(5);
stack.push(54.01);
stack.push(55);
stack.push(577);
stack.printStack();
cout<<stack.contains(54)<<endl;
cout<<stack.contains(555)<<endl;
return 0;
}
var pool = require('../common/redis_factory').pool;
var ItemModel = require('../Dao/item')();
app.get('/getData',function(req, res) {
var type = req.query.type;
var city = req.query.city;
var id = type + city;//根据参数生成id
pool.acquire(function (err, client) {//pool.acquire获取数据库连接
client.GET(id, function (err,result) {//在redis里面看看id对应的值有没存在
if(result!=null){
pool.release(client);//释放数据库连接
res.end(result);//存在,返回数据
}else{
ItemModel.findByType(type,city,function(err,data){//不存在,去mongodb里面查询
if(!err) {
var ret =JSON.stringify(data);//如果不出错,把返回的json转化为字符串
pool.acquire(function (err, client) {
client.SET(id,ret,function (err,result) {//把字符串存到redis里面
pool.release(client);//释放数据库连接
});
});
res.end(ret);//返回查询到的数据
}
else{
res.end(err);
}
});
}
}
});
});
然后自己实现了node api数据的缓存
我先写了个模块,当做redis的链接对象的工厂
新建了redis_factory.js
var redis = require('redis');
var poolModule = require('generic-pool');//使用generic-pool池化
var pool = poolModule.Pool({
name : 'redisPool',
create : function(callback) {
var client = redis.createClient();
callback(null, client);
},
destroy : function(client) {
client.quit();
},
max : 100,
min : 5,
idleTimeoutMillis : 30000,
log : false
});
exports.pool = pool;
然后在api方法里面这样用
#include <iostream>
#include"stack.h"
#include"stack.cpp"
using namespace std;
int main()
{
Stack<double> stack;
stack.push(5);
stack.push(54.01);
stack.push(55);
stack.push(577);
stack.printStack();
cout<<stack.contains(54)<<endl;
cout<<stack.contains(555)<<endl;
return 0;
}
var pool = require('../common/redis_factory').pool;
var ItemModel = require('../Dao/item')();
app.get('/getData',function(req, res) {
var type = req.query.type;
var city = req.query.city;
var id = type + city;//根据参数生成id
pool.acquire(function (err, client) {//pool.acquire获取数据库连接
client.GET(id, function (err,result) {//在redis里面看看id对应的值有没存在
if(result!=null){
pool.release(client);//释放数据库连接
res.end(result);//存在,返回数据
}else{
ItemModel.findByType(type,city,function(err,data){//不存在,去mongodb里面查询
if(!err) {
var ret =JSON.stringify(data);//如果不出错,把返回的json转化为字符串
pool.acquire(function (err, client) {
client.SET(id,ret,function (err,result) {//把字符串存到redis里面
pool.release(client);//释放数据库连接
});
});
res.end(ret);//返回查询到的数据
}
else{
res.end(err);
}
});
}
}
});
});
相关文章推荐
- 第二人生的源码分析(六十七)LLXMLNode使用Expat库打开文件
- TreeNode 使用小节
- HTMLParser使用详解(2)- Node内容
- ExtJS学习笔记(四)使用树控件TreeNode ,TreeLoader
- 使用hadoop过程中两次datanode无法启动问题
- Irrlicht 的 ISceneNode::getAbsolutePosition()的使用注意事项
- [VB.NET]vb.net中使用treenodecollection的add 方法出错
- 将Html原码解析成IHTMLDocumet对象,然后使用DOMNode将html显示成一棵树(转载
- HTMLParser使用详解(2)- Node内容
- 学习使用TreeView 控件的TreeNode 对象来获取选中了CheckBox的节点!
- 第二人生的源码分析(六十八)LLXMLNode使用Expat库分析XML文件
- 蔡军生先生第二人生的源码分析(六十八)LLXMLNode使用Expat库分析XML文件
- 将Html原码解析成IHTMLDocumet对象,然后使用DOMNode将html显示成一棵树
- 第二人生的源码分析(六十八)LLXMLNode使用Expat库分析XML文件
- NetBeans RCP - 使用 Node & ExplorerManager 结合 BeanTreeView 编写一个磁盘文件树
- 将Html原码解析成IHTMLDocumet对象,然后使用DOMNode将html显示成一棵树
- 第二人生的源码分析(六十七)LLXMLNode使用Expat库打开文件
- 使用msxml遍历重复的node问题
- jstree详解(解决jstree树的sort和move_node和drop drag一起使用时不能排序的问题)(附源码)
- HTMLParser使用详解(2)- Node内容