Node-redis: ストリーミングサーバーの応答

作成日 2016年12月14日  ·  6コメント  ·  ソース: NodeRedis/node-redis

こんにちは、 node_redisを使用して、 HMGETKEYSなどのコマンドでサーバー応答をストリーミングすることは可能ですか?

ありがとう。

Feature Request

全てのコメント6件

:+1:実装があれば素晴らしいと思います

@aletorrado私はすでに同様の状況にあり、HKEYScmdからストリーミングしたかった
そのため、HSCAN cmdを使用して、この実装で読み取り可能なストリームを作成しました。

const redis = require('redis');
const connectionParams = {};

class HKeysReadable extends require('stream').Readable {
  constructor(key, chunkSize, opt) {
    opt = opt || {};
    opt.objectMode = true;
    super(opt);
    this._cursor = 0;
    this.key = key;
    this.chunkSize = chunkSize;
    this.client = redis.createClient(connectionParams);
  }

  _read() {
    if (this._cursor === '0') {
      this.push(null);
      return this.client.quit();
    }

    this.client.hscan(this.key, this._cursor, 'COUNT', this.chunkSize, (err, res) => {
        if (err) {
          this.push(null);
          this.client.quit();
          return process.nextTick(() => this.emit('error', err));
        }

        this._cursor = res[0];

        /**
         *
         SCAN returns value as an array of two values, the first value is the new cursor to use in the next call,
         the second value is an array of elements like: [key1, val1, key2, val2, key3, val3, .....]
         In this stream, we need the keys only so we reduce that array to get only the odd values which contain the keys

         */
        let keys = res[1].reduce((accumulator, val, index)=> {
          if (!(index % 2)) {
            accumulator.push(val);
          }

          return accumulator;
        }, []);

        if (keys.length > 0) {
          this.push(keys);
        }
      }
    );
  }
};

@BridgeARどう思いますか? Redisライブラリでも同様の実装を適応させる可能性はありますか?

素晴らしい@ hossam-運賃。 基本的に、ホワイトリストに登録された一連のredisコマンド(SCAN、SSCAN、HSCAN、ZSCAN)の場合、 node_redisクライアントからカーソルを抽象化できます。

@ hossam-fares私は間違いなく同様の何かを実装してもらいたいです:+1:

@BridgeARかっこいい、PRに取り組みます

これは実装されていますか?

このページは役に立ちましたか?
0 / 5 - 0 評価