我有两个相同的应用程序,一个用于演示,一个用于开发。 我使用Redis数据库存储键值,如何为这两个不同的应用程序分离Redis数据库。 我正在使用Node.js作为Redis客户端,并且正在使用此https://github.com/mranney/node_redis/ Redis客户端。
如何在Node中将相同应用程序的Redis数据库分开。
我有两个相同的应用程序,一个用于演示,一个用于开发。 我使用Redis数据库存储键值,如何为这两个不同的应用程序分离Redis数据库。 我正在使用Node.js作为Redis客户端,并且正在使用此https://github.com/mranney/node_redis/ Redis客户端。
如何在Node中将相同应用程序的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
});
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()
,就可以设置production
或development
的选项。
db.select(app.get('redisdb'), function(err,res){ // app.get will return the value you set above
// do something here
});
有关在expressjs中的开发/生产的更多信息:http://expressjs.com/guide.html#configuration
node_redis
.select(db, callback)
回调函数将在第二个参数中返回OK,如果选择了数据库。可以在 node_redis readme 的 Usage 部分看到示例。