From a1d0842a447609f5a6cb8c8c8207fdfaaab86885 Mon Sep 17 00:00:00 2001 From: qmqz Date: Thu, 12 Mar 2026 09:11:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2dowhile=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/base/database/impl/MongoDb.dart | 10 +++++++--- lib/base/redis/redis.dart | 27 +++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/base/database/impl/MongoDb.dart b/lib/base/database/impl/MongoDb.dart index 7d7d3bf..e8f6df3 100644 --- a/lib/base/database/impl/MongoDb.dart +++ b/lib/base/database/impl/MongoDb.dart @@ -23,9 +23,13 @@ class MongoDb implements DataBase { await db.open(); print('MongoDb Connected successfully!'); //定时检测数据库是否断开 - do { - await Future.delayed(Duration(seconds: 5)); - } while (db.isConnected); + // do { + // await Future.delayed(Duration(seconds: 5)); + // } while (db.isConnected); + await Future.doWhile(() async { + await Future.delayed(Duration(seconds: 1)); + return db.isConnected; + }); connect(true); } catch (e) { print('MongoDb Connection error: $e'); diff --git a/lib/base/redis/redis.dart b/lib/base/redis/redis.dart index ae5165b..863f1c7 100644 --- a/lib/base/redis/redis.dart +++ b/lib/base/redis/redis.dart @@ -51,12 +51,12 @@ class Redis { _connected = true; //定时检测是否断开连接 Future.delayed(Duration(seconds: 1), () async { - do { + await Future.doWhile(() async { await Future.delayed(Duration(seconds: 5)); try { var r = await _command!.send_object(["PING"]); if (r != "PONG") { - break; + return false; } } catch (e) { //发送数据失败 @@ -66,9 +66,28 @@ class Redis { } catch (o) { // } - break; + return false; } - } while (true); + return true; + }); + // do { + // await Future.delayed(Duration(seconds: 5)); + // try { + // var r = await _command!.send_object(["PING"]); + // if (r != "PONG") { + // break; + // } + // } catch (e) { + // //发送数据失败 + // try { + // print('Redis Connection check failed: $e'); + // _command!.get_connection().close(); + // } catch (o) { + // // + // } + // break; + // } + // } while (true); connect(reconnect: true); }); } catch (e) {