How to separate redis database for same two app in node.js

别等时光非礼了梦想. 提交于 2019-12-03 12:20:56

问题


I have two same app running on different one for demo and one for developement .and m using the redis database to store key value, how can i seperate redis database for these two different app. m using node.js for redis client. and m using this https://github.com/mranney/node_redis/ redis client.

how to seperate redis database for same app in node.


回答1:


You can use the .select(db, callback) function in node_redis.

var redis = require('redis'),
db = redis.createClient();

db.select(1, function(err,res){
  // you'll want to check that the select was successful here
  // if(err) return err;
  db.set('key', 'string'); // this will be posted to database 1 rather than db 0
});

If you are using expressjs, you can set a development and production environment variable to automatically set which database you are using.

var express = require('express'), 
app = express.createServer();

app.configure('development', function(){
  // development options go here
  app.set('redisdb', 5);
});

app.configure('production', function(){
  // production options here
  app.set('redisdb', 0);
});

Then you can make one call to db.select() and have the options set for production or development.

db.select(app.get('redisdb'), function(err,res){ // app.get will return the value you set above
  // do something here
});

More information on dev/production in expressjs: http://expressjs.com/guide.html#configuration

The node_redis .select(db, callback) callback function will return OK in the second argument if the database is selected. An example of this can be seen on the Usage section of the node_redis readme.



来源:https://stackoverflow.com/questions/6304955/how-to-separate-redis-database-for-same-two-app-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!