Node-redis: Resposta do servidor de streaming

Criado em 14 dez. 2016  ·  6Comentários  ·  Fonte: NodeRedis/node-redis

Olá, é possível com node_redis transmitir a resposta do servidor com comandos como HMGET ou KEYS ?

Obrigado.

Feature Request

Todos 6 comentários

: +1: seria ótimo se tivéssemos uma implementação para ele

@aletorrado já passei por situação parecida e queria fazer stream do cmd HKEYS
então criei um fluxo legível usando HSCAN cmd, com esta implementação:

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 o que você acha? existe a possibilidade de adaptar a implementação semelhante na biblioteca Redis também?

Ótimo @hossam-fares. Basicamente, ele pode abstrair cursores de clientes node_redis , para um conjunto de comandos redis na lista de permissões (ou seja, SCAN, SSCAN, HSCAN, ZSCAN).

@ hossam-fares, com certeza gostaria de implementar algo semelhante: +1:

@BridgeAR legal, vou trabalhar em uma RP para isso

Isso foi implementado?

Esta página foi útil?
0 / 5 - 0 avaliações

Questões relacionadas

twappworld picture twappworld  ·  7Comentários

jackycchen picture jackycchen  ·  4Comentários

shmendo picture shmendo  ·  6Comentários

Mickael-van-der-Beek picture Mickael-van-der-Beek  ·  6Comentários

gpascale picture gpascale  ·  4Comentários