如何在node.js中将相同的两个应用程序的redis数据库分开

我有两个相同的应用程序运行在不同的演示和一个开发。和m使用redis数据库来存储键值,我怎样才能分开这两个不同的应用程序的Redis数据库。 m使用node.js作为redis客户端。 和m使用这个https://github.com/mranney/node_redis/ redis客户端。

如何在节点中为相同的应用程序分隔redis数据库。

您可以使用.select(db, callback)函数。

 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 }); 

如果您使用expressjs,则可以设置开发和生产环境variables来自动设置您正在使用的数据库。

 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); }); 

然后,您可以调用db.select()并为productiondevelopment设置选项。

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

有关expressjs中dev / production的更多信息: http ://expressjs.com/guide.html#configuration

如果select了数据库,则node_redis .select(db, callback)callback函数将在第二个参数中返回OK。 可以在node_redis自述文件的用法部分中看到这方面的一个示例。