diff --git a/Server/application/command/SwitchFriendsCommand.php b/Server/application/command/SwitchFriendsCommand.php index d153f372..2ea77512 100644 --- a/Server/application/command/SwitchFriendsCommand.php +++ b/Server/application/command/SwitchFriendsCommand.php @@ -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); + } + } + } \ No newline at end of file diff --git a/Server/application/common.php b/Server/application/common.php index d412b53a..5b6987ae 100644 --- a/Server/application/common.php +++ b/Server/application/common.php @@ -526,3 +526,29 @@ function dump() { call_user_func_array(['app\\common\\helper\\Debug', 'dump'], func_get_args()); } + +if (!function_exists('artificialAllotWechatFriend')) { + function artificialAllotWechatFriend($friend = []) + { + if (empty($friend)) { + return false; + } + + //记录切换好友 + $cacheKey = 'allotWechatFriend'; + $cacheFriend = $friend; + $cacheFriend['time'] = time() + 120; + $maxRetry = 5; + $retry = 0; + do { + $cacheFriendData = Cache::get($cacheKey, []); + // 去重:移除同friendId的旧数据 + $cacheFriendData = array_filter($cacheFriendData, function($item) use ($cacheFriend) { + return $item['friendId'] !== $cacheFriend['friendId']; + }); + $cacheFriendData[] = $cacheFriend; + $success = Cache::set($cacheKey, $cacheFriendData); + $retry++; + } while (!$success && $retry < $maxRetry); + } +} \ No newline at end of file