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]); }