基本问题:使用 Node.js 我想获取我的 redis 数据库中的所有键。当我调用 keys *;
时,我的 redis 数据库看起来像这样- aXF
- x9U
- 好的
所以我拥有的每条记录都有一个唯一的键,生成为随机字符串。现在我想调用类似 foreach(key in Redis) 并获取 redis 中的所有键。是否可以使用 Node.js 和 Redis 完成类似 "SELECT * FROM Redis"的查询
最佳答案
当然,您需要为 nodejs
安装 redis
模块,该模块位于 https://github.com/redis/node-redis .
npm install redis
那么你会这样做:
var redis = require('redis'),
client = redis.createClient();
client.keys('*', function (err, keys) {
if (err) return console.log(err);
for(var i = 0, len = keys.length; i < len; i++) {
console.log(keys[i]);
}
});
一般来说,您不希望总是返回所有键(对于较大的数据集,性能会很差),但如果您只是在进行测试,这将起作用。 Redis
文档中甚至还有一个很好的警告:
Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.
关于node.js - Redis 和 Node.js : All keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12793938/