Skip to content

Commit

Permalink
cache for contollers implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePatrykOOO committed Mar 9, 2023
1 parent b9c3190 commit 67240db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;

protected const CACHE_TTL = 3600;
}
34 changes: 23 additions & 11 deletions app/Http/Controllers/DepartmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Repositories\DepartmentRepository;
use App\Services\Departments\DepartmentReportService;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -25,8 +26,10 @@ public function __construct(DepartmentRepository $departmentRepository)
*/
public function index(): AnonymousResourceCollection
{
$listOfDepartments = $this->departmentRepository->findAll();
return DepartmentResource::collection($listOfDepartments);
return Cache::remember("departments", self::CACHE_TTL, function () {
$listOfDepartments = $this->departmentRepository->findAll();
return DepartmentResource::collection($listOfDepartments);
});
}

/**
Expand All @@ -43,8 +46,10 @@ public function store(DepartmentRequest $request): DepartmentResource
*/
public function show(int $departmentId): DepartmentResource
{
$department = $this->departmentRepository->findOrFail($departmentId);
return new DepartmentResource($department);
return Cache::remember("department.$departmentId", self::CACHE_TTL, function () use ($departmentId) {
$department = $this->departmentRepository->findOrFail($departmentId);
return new DepartmentResource($department);
});
}

/**
Expand All @@ -69,12 +74,19 @@ public function generateReportListOfEmployees(
int $departmentId,
string $reportType,
DepartmentReportService $departmentReportService
): JsonResponse {
$reportType = ReportType::tryFrom($reportType);
if (!$reportType) {
return new JsonResponse(['message' => "Report Type not found"], Response::HTTP_BAD_REQUEST);
}
$reportData = $departmentReportService->generateReport($departmentId, $reportType);
return new JsonResponse($reportData, Response::HTTP_CREATED);
): JsonResponse
{
return Cache::remember("department.$departmentId.$reportType", self::CACHE_TTL, function () use (
$departmentReportService,
$reportType,
$departmentId
) {
$reportType = ReportType::tryFrom($reportType);
if (!$reportType) {
return new JsonResponse(['message' => "Report Type not found"], Response::HTTP_BAD_REQUEST);
}
$reportData = $departmentReportService->generateReport($departmentId, $reportType);
return new JsonResponse($reportData, Response::HTTP_CREATED);
});
}
}
13 changes: 9 additions & 4 deletions app/Http/Controllers/EmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Resources\EmployeeResource;
use App\Repositories\EmployeeRepository;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -23,8 +24,10 @@ public function __construct(EmployeeRepository $employeeRepository)
*/
public function index(): AnonymousResourceCollection
{
$listOfEmployees = $this->employeeRepository->findAll();
return EmployeeResource::collection($listOfEmployees);
return Cache::remember("employees", self::CACHE_TTL, function () {
$listOfEmployees = $this->employeeRepository->findAll();
return EmployeeResource::collection($listOfEmployees);
});
}

/**
Expand All @@ -41,8 +44,10 @@ public function store(EmployeeRequest $request): EmployeeResource
*/
public function show(int $employeeId): EmployeeResource
{
$employee = $this->employeeRepository->findOrFail($employeeId);
return new EmployeeResource($employee);
return Cache::remember("employee.$employeeId", self::CACHE_TTL, function () use ($employeeId) {
$employee = $this->employeeRepository->findOrFail($employeeId);
return new EmployeeResource($employee);
});
}

/**
Expand Down

0 comments on commit 67240db

Please sign in to comment.