From 6c58ba44c7802e93e43a6dfd090cd02a5c4b98fb Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 4 Dec 2024 10:39:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=BC=BA=20RedisBinLogCurrentS?= =?UTF-8?q?napshot=20=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E6=B8=85=E9=99=A4=E6=97=A0=E6=95=88=E7=BC=93=E5=AD=98?= =?UTF-8?q?=20(#765)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 增强 RedisBinLogCurrentSnapshot 的错误处理,清除无效缓存 * fix: 增强 RedisBinLogCurrentSnapshot 的错误处理,添加日志记录并重命名无效缓存 * fix: 更新 RedisBinLogCurrentSnapshot 的错误处理,改进日志记录以包含备份文件名 --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- .../Snapshot/RedisBinLogCurrentSnapshot.php | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/trigger/src/Snapshot/RedisBinLogCurrentSnapshot.php b/src/trigger/src/Snapshot/RedisBinLogCurrentSnapshot.php index e3fb2d808..e4ef67b19 100644 --- a/src/trigger/src/Snapshot/RedisBinLogCurrentSnapshot.php +++ b/src/trigger/src/Snapshot/RedisBinLogCurrentSnapshot.php @@ -12,8 +12,11 @@ namespace FriendsOfHyperf\Trigger\Snapshot; use FriendsOfHyperf\Trigger\Consumer; +use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Redis\Redis; use MySQLReplication\BinLog\BinLogCurrent; +use RuntimeException; +use Throwable; use function Hyperf\Support\with; @@ -21,7 +24,8 @@ class RedisBinLogCurrentSnapshot implements BinLogCurrentSnapshotInterface { public function __construct( private Consumer $consumer, - private Redis $redis + private Redis $redis, + private StdoutLoggerInterface $logger, ) { } @@ -34,13 +38,24 @@ public function set(BinLogCurrent $binLogCurrent): void public function get(): ?BinLogCurrent { return with($this->redis->get($this->key()), function ($data) { - $data = unserialize((string) $data); + try { + $data = unserialize((string) $data); + + if (! $data instanceof BinLogCurrent) { + throw new RuntimeException('Invalid BinLogCurrent cache.'); + } + + return $data; + } catch (Throwable $e) { + $this->redis->rename( + $this->key(), + $key = $this->key() . '.bak_' . date('YmdHis') + ); + + $this->logger->warning('BinLogCurrent cache invalid, rename to ' . $key); - if (! $data instanceof BinLogCurrent) { return null; } - - return $data; }); }