【发布时间】:2022-06-16 15:49:29
【问题描述】:
首先,让我告诉你我是如何在我的 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;
}
}
出于某种原因 - 我不知道 - 我的应用程序有时会无缘无故地崩溃并给出错误 - 这种情况大约每周发生一次或两次:
Error: Socket closed unexpectedly
现在,我的问题:
- 这样使用 Redis 连接好吗?我的方法有问题吗?
- 为什么会发生这种情况?为什么我的套接字会意外关闭?
- 有没有办法捕获这个错误(使用我的方法)或任何其他实现 Redis 连接的良好做法?
【问题讨论】:
标签: node.js sockets redis node-redis