redis 新增密码和db选择

This commit is contained in:
2026-01-21 09:45:50 +08:00
parent a34d58f93e
commit e21033bc10

View File

@@ -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,
});
}