Browse Source

🐞 fix: 修复redis配置错误的问题

Pchen. 4 days ago
parent
commit
efab1710c7
1 changed files with 14 additions and 8 deletions
  1. 14 8
      plugin/DataBase/Redis.js

+ 14 - 8
plugin/DataBase/Redis.js

@@ -5,18 +5,24 @@ const path = require('path');
 
 
 class RedisClient {
 class RedisClient {
     constructor() {
     constructor() {
-        // 创建 Redis 客户端实例
-        this.client = redis.createClient({
-            host: config.redis.host,
-            port: config.redis.port,
-            password: config.redis.password || null
-        });
+        // node-redis v4 必须使用 socket 或 url,顶层 host/port 会被忽略并默认连 127.0.0.1:6379
+        const clientOptions = {
+            socket: {
+                host: config.redis.host,
+                port: config.redis.port
+            }
+        };
+        if (config.redis.password) {
+            clientOptions.password = config.redis.password;
+        }
+        this.client = redis.createClient(clientOptions);
 
 
         this.logger = new Logger(path.join(__dirname, '../../logs/Redis.log'), 'INFO');
         this.logger = new Logger(path.join(__dirname, '../../logs/Redis.log'), 'INFO');
+        this.logger.info(`Redis 目标:${config.redis.host}:${config.redis.port}`);
 
 
         // 处理连接错误
         // 处理连接错误
         this.client.on('error', (err) => {
         this.client.on('error', (err) => {
-            this.logger.error(`Redis连接出错:${err.stack}`);
+            this.logger.error(`Redis连接出错:${err?.message || err?.stack || err}`);
         });
         });
 
 
         // 确保连接成功
         // 确保连接成功
@@ -39,7 +45,7 @@ class RedisClient {
     getClient() {
     getClient() {
         if (!this.client.isOpen) {
         if (!this.client.isOpen) {
             this.client.connect().catch((err) => {
             this.client.connect().catch((err) => {
-                this.logger.error(`重新连接Redis时出错:${err.stack}`);
+                this.logger.error(`重新连接Redis时出错:${err?.message || err?.stack || err}`);
             });
             });
         }
         }
         return this.client;
         return this.client;