12

デモ用と開発用に 2 つの同じアプリを実行しています。また、キー値を格納するために redis データベースを使用しています。これら 2 つの異なるアプリの redis データベースを分離するにはどうすればよいですか。m redis クライアントに node.js を使用します。そして、このhttps://github.com/mranney/node_redis/ redis クライアントを使用します。

ノード内の同じアプリの redis データベースを分離する方法。

4

1 に答える 1

26

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

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

次に、 を 1 回呼び出して、またはdb.select()のオプションを設定できます。 productiondevelopment

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)コールバック関数は 2 番目の引数で OK を返します。この例は、 node_redis readmeの使用セクションで見ることができます。

于 2011-06-10T10:51:53.503 に答える