【问题标题】:How to set a new key Redis callback如何设置新的键 Redis 回调
【发布时间】:2019-01-22 22:41:41
【问题描述】:

我可以在 Redis 中设置一个键:

client.set("tmpkey", 100, "EX", 100);

但是在另一个 Redis 函数(KEYS 或 SCAN)的回调中调用此函数时不会设置键:

var client = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);          

client.on('error', function (err) {
    console.log(err);
});

client.keys("TAG:*", function(err, res) {
    console.log(res);
    client.set("TMP", 100, "EX", 100);
});

client.quit(function (err, res) {
  console.log('Exiting from quit command.');
});

扫描现有键后如何在 Redis 中设置值?

【问题讨论】:

  • 您是否收到错误消息?你在哪里定义客户?
  • 没有错误信息,根本没有设置key

标签: node.js redis node-redis


【解决方案1】:

用你的 redis 库来猜一猜。应该也一样。

但是这个函数不一样

它有什么作用?

【讨论】:

  • 我正在使用 node-redis。
  • 它根本没有设置那个键。不幸的是,它不会返回任何错误消息
  • 我猜 KEYS 命令的工作时间太长了,所以你应该等待很长时间才能执行下一行代码。您的数据库中有多少个键?
【解决方案2】:

原来client.quit()是在client.keys()函数回调完全执行之前调用的。

将 client.quit() 移动到 client.keys() 的回调中为我解决了这个问题。

client.keys("TAG:*", function(err, res) {
  console.log(res);
  client.set("TMP", 100, "EX", 100);

  client.quit(function (err, res) {
    console.log('Exiting from quit command.');
  });
});

但更好的解决方案是使用 Promises。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多