From e935820570fff7ad4f15e40d408eb97700744037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Mon, 21 Oct 2024 09:02:25 +0200 Subject: [PATCH] Add a separate pool to keep iss_data --- config/packages/cache.yaml | 5 +++-- src/Controller/MainController.php | 12 +++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index 6899b72..74b1d94 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -15,5 +15,6 @@ framework: #app: cache.adapter.apcu # Namespaced pools use the above "app" backend by default - #pools: - #my.dedicated.cache: null + pools: + iss_location: + default_lifetime: 120 diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index d58e768..869db4b 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -6,6 +6,7 @@ use App\Repository\StarshipRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; class MainController extends AbstractController @@ -13,13 +14,14 @@ class MainController extends AbstractController #[Route('/', name: 'app_homepage')] public function homepage( HttpClientInterface $client, - StarshipRepository $repository): Response - { + StarshipRepository $repository, + CacheInterface $issLocation, + ): Response { $starships = $repository->findAll(); $starship = $starships[array_rand($starships)]; - - $response = $client->request('GET', 'https://api.wheretheiss.at/v1/satellites/25544'); - $issData = $response->toArray(); + $issData = $issLocation->get('iss_data', function () use ($client): array { + return $client->request('GET', 'https://api.wheretheiss.at/v1/satellites/25544')->toArray(); + }); return $this->render('main/homepage.html.twig', ['ships' => $starships, 'myShip' => $starship, 'issData' => $issData]); }