Browse Source

✨ feat: 引入Redis

Pchen. 7 months ago
parent
commit
69c807cb6f
2 changed files with 39 additions and 0 deletions
  1. 5 0
      config-example.json
  2. 34 0
      plugin/DataBase/Redis.js

+ 5 - 0
config-example.json

@@ -7,6 +7,11 @@
         "user": "root",
         "user": "root",
         "password": ""
         "password": ""
     },
     },
+    "redis": {
+        "host": "localhost",
+        "port": 6379,
+        "password": ""
+    },
     "wxwork": {
     "wxwork": {
         "corpid": "",
         "corpid": "",
         "corpsecret": "",
         "corpsecret": "",

+ 34 - 0
plugin/DataBase/Redis.js

@@ -0,0 +1,34 @@
+const redis = require('redis');
+const config = require('../../config.json');
+const Logger = require('../../lib/Logger');
+
+class RedisClient {
+    constructor() {
+        // 创建 Redis 客户端实例
+        this.client = redis.createClient({
+            host: config.redis.host,
+            port: config.redis.port,
+            password: config.redis.password || null
+        });
+
+        this.logger = new Logger(path.join(__dirname, '../../logs/Redis.log'), 'INFO');
+
+        // 处理连接错误
+        this.client.on('error', (err) => {
+            this.logger.error(`Redis连接出错:${err.stack}`);
+        });
+
+        // 确保连接成功
+        this.client.on('connect', () => {
+            this.logger.info('Redis连接成功!');
+        });
+    }
+
+    // 获取 Redis 客户端实例
+    getClient() {
+        return this.client;
+    }
+}
+
+const Redis = new RedisClient().getClient();
+module.exports = Redis;