Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 424
XMen Avatar asked Jun 10 '11 10:06

XMen


People also ask

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

How set Redis data in node JS?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.

Why do we use different databases numbers for Redis?

Using multiple databases in a single instance may be useful in the following scenario: Different copies of the same database could be used for production, development or testing using real-time data. People may use replica to clone a redis instance to achieve the same purpose.

How many databases can Redis have?

Managing Databases. Out of the box, a Redis instance supports 16 logical databases. These databases are effectively siloed off from one another, and when you run a command in one database it doesn't affect any of the data stored in other databases in your Redis instance.


1 Answers

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.

like image 126
slickplaid Avatar answered Sep 28 '22 08:09

slickplaid