From e21033bc10973d40ff9cd4369a1efb9f7c30b0bb Mon Sep 17 00:00:00 2001 From: qmqz Date: Wed, 21 Jan 2026 09:45:50 +0800 Subject: [PATCH] =?UTF-8?q?redis=20=E6=96=B0=E5=A2=9E=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E5=92=8Cdb=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/base/redis/redis.dart | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/base/redis/redis.dart b/lib/base/redis/redis.dart index a247d1d..ae5165b 100644 --- a/lib/base/redis/redis.dart +++ b/lib/base/redis/redis.dart @@ -30,6 +30,24 @@ class Redis { try { _command = await RedisConnection().connect(_config.host, _config.port); print('Redis Connected successfully!'); + if (_config.password != null) { + var authResult = + await _command!.send_object(['AUTH', _config.password]); + if (authResult == "OK") { + print('Redis Authentication successfully'); + } else { + print('Redis Authentication failed'); + connect(reconnect: reconnect); + } + } + if (_config.database != 0) { + var result = await _command! + .send_object(["SELECT", _config.database.toString()]); + if (result == "OK") { + print('Redis use db ${_config.database}'); + } + } + _connected = true; //定时检测是否断开连接 Future.delayed(Duration(seconds: 1), () async { @@ -152,9 +170,13 @@ class Redis { class RedisConfig { String host; int port; + String? password; + int database; RedisConfig({ required this.host, this.port = 6379, + this.password, + this.database = 0, }); }