Skip to content

Commit

Permalink
fix: 增强 RedisBinLogCurrentSnapshot 的错误处理,清除无效缓存 (#765)
Browse files Browse the repository at this point in the history
* fix: 增强 RedisBinLogCurrentSnapshot 的错误处理,清除无效缓存

* fix: 增强 RedisBinLogCurrentSnapshot 的错误处理,添加日志记录并重命名无效缓存

* fix: 更新 RedisBinLogCurrentSnapshot 的错误处理,改进日志记录以包含备份文件名

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Dec 4, 2024
1 parent 6655825 commit 6c58ba4
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/trigger/src/Snapshot/RedisBinLogCurrentSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
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;

class RedisBinLogCurrentSnapshot implements BinLogCurrentSnapshotInterface
{
public function __construct(
private Consumer $consumer,
private Redis $redis
private Redis $redis,
private StdoutLoggerInterface $logger,
) {
}

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

Expand Down

0 comments on commit 6c58ba4

Please sign in to comment.