Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] scan keys on redis cluster #1282

Merged
merged 4 commits into from
Apr 8, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/services/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ class Redis extends Service {
* @returns {Promise} promise resolving to an array of keys
*/
searchKeys(pattern) {
return new Bluebird(resolve => {
if (this._client instanceof IORedis.Cluster) {
let keys = [];
const stream = this._client.scanStream({match: pattern});
const promises = [];

stream.on('data', resultKeys => {
keys = keys.concat(resultKeys);
});
for (const node of this._client.nodes('master')) {
promises.push(this._searchNodeKeys(node, pattern)
.then(nodeKeys => { // eslint-disable-line no-loop-func
keys = keys.concat(nodeKeys);
}));
}

stream.on('end', () => {
resolve(_.uniq(keys));
});
});
return Bluebird.all(promises)
.then(() => _.uniq(keys));
}

return this._searchNodeKeys(this._client, pattern);
}

/**
Expand Down Expand Up @@ -205,6 +209,23 @@ class Redis extends Service {

return this._client.mget(_args);
}

_searchNodeKeys(node, pattern) {
return new Bluebird(resolve => {
let keys = [];
const stream = node.scanStream({match: pattern});

stream.on('data', resultKeys => {
keys = keys.concat(resultKeys);
});

stream.on('end', () => {
resolve(_.uniq(keys));
});
});

}

}

module.exports = Redis;
Expand Down