node.js - Redis 和 Node.js : All keys

标签 node.js redis

基本问题:使用 Node.js 我想获取我的 redis 数据库中的所有键。当我调用 keys *;

时,我的 redis 数据库看起来像这样
  1. aXF
  2. x9U
  3. 好的

所以我拥有的每条记录都有一个唯一的键,生成为随机字符串。现在我想调用类似 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/

相关文章:

angularjs - 在 AngularJS 和 NodeJS 中 stash API key 并推送到 Git 和 Heroku

javascript - 如何停止在 MySQL 中舍入 int 值

javascript - 带有异步子任务的异步游标迭代

python - redis-py ttl 返回无,但 redis 文档引用 -1

使用 SSL 时 Azure Redis 连接失败

php - 用于 PHP session 的 Redis - 为什么 Web 服务器上有这么多 session ,而本地只有一个?

node.js - 在 express 中从 POST 请求处理程序发送 WebSocket 消息

node.js - 如何在 Mongoose 模式中设置大于 0 的最小值?

Redis active - 跨数据中心/Kubernetes 集群的主动复制

postgresql - 一批从 PostgreSQL 传输到 Redis 的数据有多少被认为是可靠的?