javascript - 如何在nodejs redis中递归扫描键
问题描述
根据这里提出的解决方案,我编写了这个函数来递归扫描 NodeJS Redis 中的键 -给定模式的node-redis :
RedisStore.prototype.scan = function(params, callback, cursor = '0', returnSet = new Set()) {
var self = this;
var options = {
pattern: '',
match: 'MATCH',
count: 100
};
for (var attrname in params) {
options[attrname] = params[attrname];
}
var count = '' + options.count;
self.client.scan(cursor, options.match, options.pattern, 'COUNT', count,
(err, reply) => {
if (err) {
return callback(err, null);
}
cursor = reply[0];
if (cursor === '0') { // scan completed
return callback(null, Array.from(returnSet));
} else {
var keys = reply[1];
keys.forEach(function(key, i) {
returnSet.add(key);
});
return self.scan(options, callback, cursor, returnSet);
}
});
} // scan
我之前插入了一个带有前缀test:
+ 一些字符串的键,但正在扫描
var res = await store.scan({
pattern: 'test:*',
count: 10
});
将具有作为起始值cursor = '0'
的returnSet = new Set()
, 似乎没有找到任何东西,因此导致为空resultSet
且未达到0
光标退出条件。为什么?
解决方案
推荐阅读
- c++ - argv 上的 const char * 与 const char **
- javascript - 在 javascript datepicker (asp.net) 中禁用过去的日期
- protractor - 用于边界值分析的量角器 Reusbale 函数
- excel - 使用 office js web 插件将 json 加载到 excel 中
- php - 关于使用 REGEX 在 PHP 中搜索字符串重合的建议
- ruby-on-rails - Rails:清理控制器中的大型方法
- javascript - 护照会话清除 req.session
- drools - 异常的一些可能原因是什么:“找不到 KieModule”?
- javascript - 使用 javascript 高阶函数简化对象转换
- python-3.x - PRNG 的种子大小(以位为单位)有什么不同吗?