Hallo, ist es mit node_redis
, die Serverantwort mit Befehlen wie HMGET
oder KEYS
zu streamen?
Vielen Dank.
:+1: Es wäre toll, wenn wir eine Implementierung dafür haben
@aletorrado Ich hatte schon eine ähnliche Situation und wollte von HKEYS cmd streamen
Also habe ich mit HSCAN cmd einen lesbaren Stream mit dieser Implementierung erstellt:
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 was meinst du? Gibt es eine Möglichkeit, eine ähnliche Implementierung auch in der Redis-Bibliothek anzupassen?
Tolle @hossam-Tarife. Grundsätzlich kann es Cursors von node_redis
Clients abstrahieren, für einen Satz von Redis-Befehlen auf der Whitelist (dh SCAN, SSCAN, HSCAN, ZSCAN).
@hossam-fares Ich möchte auf jeden Fall etwas Ähnliches implementiert haben :+1:
@BridgeAR cool, ich werde an einer PR dafür arbeiten
Wurde dies umgesetzt?