【问题标题】:How to check if Redis in nodejs app is working如何检查 nodejs 应用程序中的 Redis 是否正常工作
【发布时间】:2021-02-18 07:47:34
【问题描述】:

我在 VM 中安装了 redis 和 mongodb,我正在尝试加快我的 mongoose find() 请求的响应时间。我使用了 lean(),它运行良好。然后我找到了 redis 并安装了它,然后我按照本教程在我的控制器中设置 redis : https://epsagon.com/development/using-redis-to-optimize-mongodb-queries/

我在 /services 中创建了带有连接详细信息的 cache.js,然后在我导入的控制器代码中:

const { clearKey } = require("../services/cache");

在我的 find() 中,我添加了 .cache() :

await Book.find({ author: req.query.author }).cache();

我想知道应用程序如何知道 .cache() 函数是我的 redis 服务器,因为我只导入了“clearKey”而我没有使用它?并且性能没有加快,所以我不知道 Redis 设置是否正确并且是否正常工作。如何检查?

谢谢

【问题讨论】:

    标签: node.js mongodb api mongoose redis


    【解决方案1】:

    您在mongoose.Query.prototype.cache 中添加了缓存功能,这意味着您使用缓存功能扩展了mongoose.Query,它已自动添加到所有 Mongoose 模型中。

    关于你的第二个问题,你需要添加到你的 Redis 中:

    const client = redis.createClient({
      host: keys.redisHost, // make sure its your redis host
      port: keys.redisPort, // make sure its your redis port
      retry_strategy: () => 1000
    });
    

    当您的查询在 MongoDB 上运行时,您应该会看到一个日志:

    console.log("Response from MongoDB");
    

    当您的查询在 Redis 上运行时,您应该会看到不同的日志:

    console.log("Response from Redis");
    

    你可以在这个githubfile看到更多。

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      相关资源
      最近更新 更多