代码提交

This commit is contained in:
wong
2025-07-19 15:25:17 +08:00
parent 8d12f5a06a
commit c619291e84
2 changed files with 74 additions and 3 deletions

View File

@@ -26,6 +26,9 @@ class SwitchFriendsCommand extends Command
protected function execute(Input $input, Output $output)
{
// 清理可能损坏的缓存数据
$this->clearCorruptedCache($output);
//处理流量分过期数据
$expUserData = Db::name('workbench_traffic_config_item')
->where('expTime','<=',time())
@@ -122,7 +125,15 @@ class SwitchFriendsCommand extends Command
$output->writeln('开始执行好友切换任务...');
do {
$friends = Cache::get($cacheKey, []);
try {
$friends = Cache::get($cacheKey, []);
} catch (\Exception $e) {
// 如果缓存数据损坏,清空缓存并记录错误
$output->writeln('缓存数据损坏,正在清空缓存: ' . $e->getMessage());
Cache::rm($cacheKey);
$friends = [];
}
$toSwitch = [];
foreach ($friends as $friend) {
if (isset($friend['time']) && $friend['time'] < $now) {
@@ -196,7 +207,15 @@ class SwitchFriendsCommand extends Command
}
// 过滤掉已切换的,保留未切换和新进来的
$newFriends = Cache::get($cacheKey, []);
try {
$newFriends = Cache::get($cacheKey, []);
} catch (\Exception $e) {
// 如果缓存数据损坏,清空缓存并记录错误
$output->writeln('缓存数据损坏,正在清空缓存: ' . $e->getMessage());
Cache::rm($cacheKey);
$newFriends = [];
}
$updated = [];
foreach ($newFriends as $friend) {
$friendId = !empty($friend['friendId']) ? $friend['friendId'] : $friend['id'];
@@ -210,7 +229,13 @@ class SwitchFriendsCommand extends Command
return ($a['time'] ?? 0) <=> ($b['time'] ?? 0);
});
$success = Cache::set($cacheKey, $updated);
try {
$success = Cache::set($cacheKey, $updated);
} catch (\Exception $e) {
// 如果缓存设置失败,记录错误并继续
$output->writeln('缓存设置失败: ' . $e->getMessage());
$success = false;
}
$retry++;
} while (!$success && $retry < $maxRetry);
@@ -222,4 +247,24 @@ class SwitchFriendsCommand extends Command
$output->writeln('缓存已更新并排序');
}
/**
* 清理损坏的缓存数据
* @param Output $output
*/
private function clearCorruptedCache(Output $output)
{
$cacheKey = 'allotWechatFriend';
try {
// 尝试读取缓存,如果失败则清空
$testData = Cache::get($cacheKey, []);
if (!is_array($testData)) {
$output->writeln('缓存数据格式错误,正在清空缓存');
Cache::rm($cacheKey);
}
} catch (\Exception $e) {
$output->writeln('检测到损坏的缓存数据,正在清空: ' . $e->getMessage());
Cache::rm($cacheKey);
}
}
}