Add a separate pool to keep iss_data

This commit is contained in:
Krzysztof Płaczek
2024-10-21 09:02:25 +02:00
parent d44091fe6e
commit e935820570
2 changed files with 10 additions and 7 deletions

View File

@@ -15,5 +15,6 @@ framework:
#app: cache.adapter.apcu #app: cache.adapter.apcu
# Namespaced pools use the above "app" backend by default # Namespaced pools use the above "app" backend by default
#pools: pools:
#my.dedicated.cache: null iss_location:
default_lifetime: 120

View File

@@ -6,6 +6,7 @@ use App\Repository\StarshipRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
class MainController extends AbstractController class MainController extends AbstractController
@@ -13,13 +14,14 @@ class MainController extends AbstractController
#[Route('/', name: 'app_homepage')] #[Route('/', name: 'app_homepage')]
public function homepage( public function homepage(
HttpClientInterface $client, HttpClientInterface $client,
StarshipRepository $repository): Response StarshipRepository $repository,
{ CacheInterface $issLocation,
): Response {
$starships = $repository->findAll(); $starships = $repository->findAll();
$starship = $starships[array_rand($starships)]; $starship = $starships[array_rand($starships)];
$issData = $issLocation->get('iss_data', function () use ($client): array {
$response = $client->request('GET', 'https://api.wheretheiss.at/v1/satellites/25544'); return $client->request('GET', 'https://api.wheretheiss.at/v1/satellites/25544')->toArray();
$issData = $response->toArray(); });
return $this->render('main/homepage.html.twig', ['ships' => $starships, 'myShip' => $starship, 'issData' => $issData]); return $this->render('main/homepage.html.twig', ['ships' => $starships, 'myShip' => $starship, 'issData' => $issData]);
} }