How to separate a redis database for the same two applications in node.js

I have two identical applications running on different ones for demonstration and one for development. and m, using the redis database to store the key value, how can I separate the redis database for these two different applications. m using node.js for the redis client. and m using this https://github.com/mranney/node_redis/ redis client.

how to separate a redis database for the same application in node.

+7
source share
1 answer

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 use expressjs, you can set the development environment 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 set the parameters for production or development .

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

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

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

+17
source

All Articles