小编Yas*_*xit的帖子

重用 Redis 连接:套接字意外关闭 - node-redis

首先,让我告诉您如何在 NodeJS 应用程序中使用 Redis 连接:

  • 我使用单例类在整个应用程序中重复使用一个连接。
class RDB {

    static async getClient() {
        if (this.client) {
            return this.client
        }

        let startTime = Date.now();

        this.client = createClient({
            url: config.redis.uri
        });

        await this.client.connect();

        return this.client;
    }

}
Run Code Online (Sandbox Code Playgroud)

由于某种原因 - 我不知道 - 我的应用程序有时会崩溃,无缘无故地出现错误 - 这种情况大约每周发生一两次:

Error: Socket closed unexpectedly
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是:

  1. 像这样使用 Redis 连接可以吗?我的做法有问题吗?
  2. 为什么会出现这种情况?为什么我的套接字意外关闭?
  3. 有没有办法捕获此错误(使用我的方法)或实现 Redis 连接的任何其他良好实践?

sockets redis node.js node-redis

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

node-redis ×1

node.js ×1

redis ×1

sockets ×1