XMe*_*Men 11 redis key-value-store node.js
我有两个相同的app运行在不同的一个用于演示和一个用于开发.并且使用redis数据库来存储键值,我如何为这两个不同的应用程序分离redis数据库.m使用node.js为redis客户端.并使用此https://github.com/mranney/node_redis/ redis客户端.
如何在节点中为同一个应用程序分离redis数据库.
sli*_*aid 23
您可以.select(db, callback)在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
});
Run Code Online (Sandbox Code Playgroud)
如果您使用的是expressjs,则可以设置开发和生产环境变量以自动设置您正在使用的数据库.
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);
});
Run Code Online (Sandbox Code Playgroud)
然后你可以拨打一个电话db.select()并为production或设置选项development.
db.select(app.get('redisdb'), function(err,res){ // app.get will return the value you set above
// do something here
});
Run Code Online (Sandbox Code Playgroud)
有关表达式中dev/production的更多信息:http://expressjs.com/guide.html#configuration
该node_redis .select(db, callback)如果选择数据库回调函数将在第二个参数返回OK.可以在node_redis自述文件的" 用法"部分中看到此示例.
| 归档时间: |
|
| 查看次数: |
11779 次 |
| 最近记录: |