1

db クエリによってバックアップされる redis クエリの非同期ラッパーが必要です。redis クエリが失敗した場合は、db クエリを作成したいと考えています。db クエリが成功した場合、返されたデータを redis に追加してから返したいと考えています。この関数は node.js 内から呼び出されるため、Promise を返すには関数 (できればオブジェクトのそのようなメソッドの 1 つ) が必要です。ブルーバードの約束ライブラリを使用しており、それを使用してredisを約束しています。これもブルーバードベースのdbにmongo-gyroを使用しています。これらはどちらもスタンドアロンで動作します。

特に疑似コードでさえ、どんな助けも深く感謝しています。誤った取り扱いで

function get_something(key){
redis.get(key).done(function (res){
  if (null !== res){
    return res;  // how do I return a promise here?
  }
})
.done(function (res){
  db.find({'_id:key'}).done(function (res){
    if (null !== res){
      redis.set(key,result)  // set db value in redis
      .then(function(){
           return res;      //how do I return a promise here?
      })
    .catch()...?
    return res;  // how do I return a promise here?
    }
})
.catch...?

};

更新: 以下の関数が機能し、最後に redis または mongo からのデータが表示されます。ただし、これを node.js ハンドラーに返される promise を返すクラスのメソッドに変換することにこれまで成功していません。注意 - データのソースをキャプチャするために「バインド」を追加する必要がありました

var oid = '+++++ test oid ++++++'
var odata = {
    'story': 'once upon a time'
}
var rkey = 'objects:'+ oid
redis.getAsync(rkey).bind(this).then(function(res){ 
  if(res === null){
    this.from = 'db'                            // we got from db
    return db.findOne('objects',{'_id':oid}) 
  }  
  data = JSON.parse(res)
  this.from = 'redis'                           // we got from redis
  return data
})
.then(function(res){    
  if(res !== null && this.from == 'db'){
    data = JSON.stringify(res)
    redis.setAsync(rkey,data)
  } 
  return res
})
.then(function(res){                           // at this point, res is not a promise
  console.log('result from ' + this.from)  
  console.log(res)                              
});
4

2 に答える 2

1

イデオタイプ、元の質問と更新の私の理解から、どのソースが必要なデータを生成したかを追跡するフラグを必要とせずに目的を達成できると思います。

このようなものが動作するはずです:

function get_something(oid) {
    var rkey = 'objects:' + oid;
    return redis.getAsync(rkey).then(function(res_r) {
        if (res_r === null) {
            return Promise.cast(db.findOne('objects', {'_id': oid})).then(function(res_db) {
                redis.setAsync(rkey, res_db).fail(function() {
                    console.error('Failed to save ' + rkey + ' to redis');
                });
                return res_db;
            });
        }
        return res_r;
    }).then(function (res) {//res here is the result delivered by either redis.getAsync() or db.find()
        if (res === null) {
            throw ('No value for: ' + rkey);
        }
        return res;
    });
}

ノート:

  • oidおよびで行を修正する必要がある場合がありますrkey。ここでの私の理解は限られています。
  • mongo-gyro クエリはオプションであり、その後の redis 更新は関数全体の成功に関するアカデミックであるため、ここでのパターンは珍しいものです。
  • Promise.cast()によって返される内容によっては、ラッパーが不要な場合がありますdb.findOne()
  • これは間違いなく、Bluebird をよりよく理解している人がもう一度試すことで恩恵を受けるでしょう。
于 2014-05-02T22:36:27.890 に答える
1

.donepromise チェーンを終了します。通常、Bluebird は未処理の拒否を独自に計算するほどスマートです。

それ.thenはあなたが探しているものです:

redis.get(key).then(function(res){ res is redis .get response
     if(res === null) throw new Error("Invalid Result for key");
     return db.find({"_id":key); // had SyntaxError here, so guessing you meant this 
}).then(function(res){ // res is redis .find response
     return redis.set(key,result);
}).catch(function(k){ k.message === "Invalid Result for key",function(err){
   // handle no key found
});
于 2014-05-01T08:31:29.903 に答える