cacheService = $cacheService; } public function recordView(Post $post, string $visitorId): void { $cacheKey = "post:{$post->id}:visitor:{$visitorId}"; if (!Cache::has($cacheKey)) { DB::transaction(function () use ($post) { $post->increment('views'); }); // 设置24小时内同一访客不重复计数 Cache::put($cacheKey, true, now()->addHours(24)); } } public function getPopularPosts(int $limit = 10): array { return $this->cacheService ->tags(['posts', 'popular']) ->remember('posts:popular', function () use ($limit) { return Post::orderByDesc('views') ->limit($limit) ->get() ->map(fn($post) => [ 'id' => $post->id, 'title' => $post->title, 'views' => $post->views, ]) ->toArray(); }); } }