diff --git a/app/Http/Controllers/Web/JobController.php b/app/Http/Controllers/Web/JobController.php index 64ceff1..c513268 100644 --- a/app/Http/Controllers/Web/JobController.php +++ b/app/Http/Controllers/Web/JobController.php @@ -7,8 +7,10 @@ use App\Models\AnalyticsEvent; use App\Models\Job; use App\Models\JobCategory; use App\Repositories\JobRepository; +use App\Services\CacheService; use App\Services\JobSearchService; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; class JobController extends Controller { @@ -17,6 +19,13 @@ class JobController extends Controller private JobRepository $jobRepository ) {} + private function getCategoriesWithCount() + { + return Cache::remember('categories:with_job_count', CacheService::CACHE_CATEGORIES, function () { + return JobCategory::withCount('jobs')->orderBy('name')->get(); + }); + } + /** * Display a listing of jobs with search and filters */ @@ -44,7 +53,7 @@ class JobController extends Controller $perPage = $request->input('per_page', 20); $jobs = $this->searchService->search($filters, $perPage); - $categories = JobCategory::withCount('jobs')->orderBy('name')->get(); + $categories = $this->getCategoriesWithCount(); $filterOptions = $this->searchService->getFilterOptions(); return view('jobs.index', compact('jobs', 'categories', 'filterOptions', 'filters')); @@ -117,7 +126,7 @@ class JobController extends Controller $perPage = $request->input('per_page', 20); $jobs = $this->searchService->search($filters, $perPage); - $categories = JobCategory::withCount('jobs')->orderBy('name')->get(); + $categories = $this->getCategoriesWithCount(); $filterOptions = $this->searchService->getFilterOptions(); return view('jobs.remote', compact('jobs', 'categories', 'filterOptions', 'filters')); diff --git a/app/Services/JobSearchService.php b/app/Services/JobSearchService.php index af4adb6..39107f1 100644 --- a/app/Services/JobSearchService.php +++ b/app/Services/JobSearchService.php @@ -5,6 +5,7 @@ namespace App\Services; use App\Models\Job; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Request; class JobSearchService { @@ -16,17 +17,21 @@ class JobSearchService */ public function search(array $filters = [], int $perPage = 15): LengthAwarePaginator { - $query = Job::query() - ->with(['source', 'category', 'jobLocation']) - ->active(); + $page = Request::input('page', 1); + $cacheKey = $this->cacheService->searchResultsKey($filters, (int) $page); - // Apply filters - $this->applyFilters($query, $filters); + $cached = $this->cacheService->remember($cacheKey, CacheService::CACHE_SEARCH_RESULTS, function () use ($filters, $perPage) { + $query = Job::query() + ->with(['source', 'category', 'jobLocation']) + ->active(); - // Apply sorting - $this->applySorting($query, $filters['sort'] ?? 'date'); + $this->applyFilters($query, $filters); + $this->applySorting($query, $filters['sort'] ?? 'date'); - return $query->paginate($perPage); + return $query->paginate($perPage); + }); + + return $cached; } /**