Node-redis: 流服务器响应

创建于 2016-12-14  ·  6评论  ·  资料来源: NodeRedis/node-redis

您好,是否可以使用node_redis使用HMGETKEYS等命令流式传输服务器响应?

谢谢。

Feature Request

所有6条评论

:+1: 如果我们有实现它会很棒

@aletorrado我已经有类似的情况,我想从 HKEYS cmd 流
所以我使用 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-fares。 基本上它可以从node_redis客户端抽象游标,用于白名单的 redis 命令集(即 SCAN、SSCAN、HSCAN、ZSCAN)。

@hossam-fares 我绝对希望实现类似的东西:+1:

@BridgeAR很酷,我会为它做 PR

这已经实施了吗?

此页面是否有帮助?
0 / 5 - 0 等级