Redis 和 Node.js:所有键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12793938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:26:15  来源:igfitidea点击:

Redis & Node.js: All keys

node.jsredis

提问by Ali

Basic question: Using Node.js I would like to get all the keys in my redis db. My redis db looks like this when I call keys *;

基本问题:使用 Node.js 我想获取我的 redis 数据库中的所有键。当我调用keys *时,我的 redis 数据库看起来像这样;

  1. aXF
  2. x9U
  3. lOk
  1. aXF
  2. x9U
  3. 好的

So each record I have, has a unique key, generated as a random string. Now I would like to call something like foreach(key in Redis)and get all keys in the redis. Would it be possible to accomplish a "SELECT * FROM Redis"-likequery with Node.js & Redis

所以我拥有的每条记录都有一个唯一的密钥,生成为一个随机字符串。现在我想调用类似foreach(key in Redis)并获取 redis 中的所有键。是否可以使用 Node.js 和 Redis完成类似“SELECT * FROM Redis”的查询

回答by Bill

Sure, you'll need to install the redismodule for nodejswhich can be found at https://github.com/mranney/node_redis.

当然,您需要安装可在https://github.com/mranney/node_redis上找到的redis模块。 nodejs

npm install node_redis

Update

更新

The above command is no longer available. you can use the following:

上面的命令不再可用。您可以使用以下内容:

npm install redis

Then you would do:

那么你会这样做:

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]);
  }
});        

Generally speaking you won't want to always return all of the keys (performance will be bad for larger data sets), but this will work if you are just testing things out. There is even a nice warning in the Redisdocumentation:

一般来说,您不会希望始终返回所有键(对于较大的数据集性能会很差),但是如果您只是在测试,这将起作用。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.

警告:将 KEYS 视为仅应在生产环境中极其小心地使用的命令。当它针对大型数据库执行时,它可能会破坏性能。此命令用于调试和特殊操作,例如更改键空间布局。不要在常规应用程序代码中使用 KEYS。如果您正在寻找一种在键空间子集中查找键的方法,请考虑使用集合。

回答by Carlos Júlio

Install redis client for nodejs

为 nodejs 安装 redis 客户端

npm install redis

Then I do the following to get all key's data

然后我执行以下操作以获取所有密钥的数据

var redis =?? require('redis'),
    client =? redis.createClient();

client.multi()
    .keys('*', function (err, replies) {
        // NOTE: code in this callback is NOT atomic
        // this only happens after the the .exec call finishes.

        console.log("MULTI got " + replies.length + " replies");

        replies.forEach(function (reply, index) {
            console.log("Reply " + index + ": " + reply.toString());
            client.get(reply, function(err, data){
                    console.log(data);
            });
        });

    })
    .exec(function (err, replies) {});

回答by Arthas

npm install node_redis 

is no more available now. Use this instead -

现在不再可用。改用这个 -

npm install redis