cacheService = app('admin.cache'); $this->adminService = app('admin.service'); } /** * 缓存管理首页 * * @param Content $content * @return Content */ public function index(Content $content): Content { return $content ->title($this->title) ->description('缓存状态管理') ->body($this->buildCacheManagement()); } /** * 构建缓存管理界面 * * @return Tab */ protected function buildCacheManagement(): Tab { $tab = new Tab(); // 缓存状态标签页 $tab->add('缓存状态', $this->buildCacheStatusCard()); // 缓存操作标签页 $tab->add('缓存操作', $this->buildCacheActionsCard()); // 缓存统计标签页 $tab->add('缓存统计', $this->buildCacheStatisticsCard()); return $tab; } /** * 构建缓存状态卡片 * * @return Card */ protected function buildCacheStatusCard(): Card { $status = $this->cacheService->getStatus(); $html = '
'; foreach ($status as $typeStatus) { $statusClass = $typeStatus['status'] === 'healthy' ? 'success' : 'danger'; $statusText = $typeStatus['status'] === 'healthy' ? '正常' : '异常'; $html .= '
'; $html .= '
'; $html .= '
'; $html .= "
{$typeStatus['label']}
"; $html .= "{$statusText}"; $html .= "

{$typeStatus['description']}

"; if (isset($typeStatus['ttl'])) { $html .= "TTL: {$typeStatus['ttl']}秒
"; } if (isset($typeStatus['tags'])) { $tags = implode(', ', $typeStatus['tags']); $html .= "标签: {$tags}"; } if (isset($typeStatus['error'])) { $html .= "
{$typeStatus['error']}
"; } $html .= '
'; $html .= '
'; $html .= '
'; } $html .= '
'; return new Card('缓存状态', $html); } /** * 构建缓存操作卡片 * * @return Card */ protected function buildCacheActionsCard(): Card { $html = '
'; // 全部清理按钮 $html .= '
'; $html .= ''; $html .= '
'; // 按类型清理 $html .= '
'; $html .= '
按类型清理
'; $html .= '
'; foreach (CACHE_TYPE::cases() as $type) { $buttonClass = $type->isSafeToClear() ? 'btn-warning' : 'btn-danger'; $safeText = $type->isSafeToClear() ? '(安全)' : '(谨慎)'; $html .= '
'; $html .= "'; $html .= '
'; } $html .= '
'; $html .= '
'; // 预热缓存 $html .= '
'; $html .= '
缓存预热
'; $html .= ''; $html .= '
'; $html .= '
'; // 添加JavaScript $html .= $this->getCacheManagementScript(); return new Card('缓存操作', $html); } /** * 构建缓存统计卡片 * * @return Card */ protected function buildCacheStatisticsCard(): Card { $statistics = $this->cacheService->getStatistics(); $html = '
'; $html .= '
'; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html .= '
缓存驱动' . ($statistics['driver'] ?? 'Unknown') . '
缓存类型总数' . ($statistics['total_types'] ?? 0) . '
关键缓存类型' . ($statistics['critical_types'] ?? 0) . '
安全清理类型' . ($statistics['safe_to_clear'] ?? 0) . '
'; $html .= '
'; $html .= '
'; return new Card('缓存统计', $html); } /** * 获取缓存管理JavaScript * * @return string */ protected function getCacheManagementScript(): string { return ' '; } /** * 清理所有缓存 * * @return \Illuminate\Http\JsonResponse */ #[Post('admin-cache/clear-all')] public function clearAll() { try { $results = $this->cacheService->clearAll(); // 记录操作日志 $this->adminService->logAdminAction( ADMIN_ACTION_TYPE::CACHE_CLEAR, '清理所有缓存', ['results' => $results] ); return response()->json([ 'status' => 'success', 'message' => '所有缓存清理完成', 'data' => $results, ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => $e->getMessage(), ], 500); } } /** * 按类型清理缓存 * * @param Request $request * @return \Illuminate\Http\JsonResponse */ #[Post('admin-cache/clear-type')] public function clearByType(Request $request) { try { $type = CACHE_TYPE::from($request->input('type')); $result = $this->cacheService->clearByType($type); // 记录操作日志 $this->adminService->logAdminAction( ADMIN_ACTION_TYPE::CACHE_CLEAR, "清理{$type->getLabel()}缓存", ['type' => $type->value, 'result' => $result] ); return response()->json([ 'status' => 'success', 'message' => $result['message'], 'data' => $result, ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => $e->getMessage(), ], 500); } } /** * 预热缓存 * * @return \Illuminate\Http\JsonResponse */ #[Post('admin-cache/warmup')] public function warmup() { try { $results = $this->cacheService->warmup(); // 记录操作日志 $this->adminService->logAdminAction( ADMIN_ACTION_TYPE::CACHE_CLEAR, '预热缓存', ['results' => $results] ); return response()->json([ 'status' => 'success', 'message' => '缓存预热完成', 'data' => $results, ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => $e->getMessage(), ], 500); } } }