From 12bf90a2b4f9e18a260a6520d99a4cc37250647b Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Thu, 1 May 2025 21:46:14 -0500 Subject: [PATCH] patch: adds full page caching to TMBD & torrentio results --- .env.dist | 1 + src/Controller/SearchController.php | 33 ++++++++++++++++---------- src/Controller/TorrentioController.php | 22 +++++++++++++---- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/.env.dist b/.env.dist index 1423396..b3f0b04 100644 --- a/.env.dist +++ b/.env.dist @@ -1,3 +1,4 @@ +APP_ENV=%%app_env%% APP_SECRET="%%app_secret%%" DATABASE_URL="%%db_url%%" DOWNLOAD_DIR=%%download_dir%% diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 5ca8fa4..1b7ed03 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -9,6 +9,8 @@ use App\Search\Action\Input\SearchInput; 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\Cache\ItemInterface; final class SearchController extends AbstractController { @@ -30,20 +32,25 @@ final class SearchController extends AbstractController #[Route('/result/{mediaType}/{tmdbId}', name: 'app_search_result')] public function result( - GetMediaInfoInput $getDownloadOptionsInput, + GetMediaInfoInput $input, + CacheInterface $cache ): Response { - $result = $this->getMediaInfoHandler->handle($getDownloadOptionsInput->toCommand()); + $cacheId = sprintf("page.%s.%s", $input->mediaType, $input->tmdbId); - return $this->render('search/result.html.twig', [ - 'results' => $result, - 'filter' => [ - 'resolution' => '', - 'codec' => '', - 'provider' => '', - 'language' => '', - 'season' => 1, - 'episode' => '' - ] - ]); + return $cache->get($cacheId, function (ItemInterface $item) use ($input) { + $item->expiresAt(new \DateTimeImmutable("today 11:59 pm")); + $result = $this->getMediaInfoHandler->handle($input->toCommand()); + return $this->render('search/result.html.twig', [ + 'results' => $result, + 'filter' => [ + 'resolution' => '', + 'codec' => '', + 'provider' => '', + 'language' => '', + 'season' => 1, + 'episode' => '' + ] + ]); + }); } } diff --git a/src/Controller/TorrentioController.php b/src/Controller/TorrentioController.php index e34e269..2bd25f4 100644 --- a/src/Controller/TorrentioController.php +++ b/src/Controller/TorrentioController.php @@ -9,6 +9,8 @@ use App\Torrentio\Action\Input\GetTvShowOptionsInput; 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\Cache\ItemInterface; final class TorrentioController extends AbstractController { @@ -28,12 +30,22 @@ final class TorrentioController extends AbstractController } #[Route('/torrentio/tvshows/{tmdbId}/{imdbId}/{season?}/{episode?}', name: 'app_torrentio_tvshows')] - public function tvShowOptions(GetTvShowOptionsInput $input): Response + public function tvShowOptions(GetTvShowOptionsInput $input, CacheInterface $cache): Response { - $results = $this->getTvShowOptionsHandler->handle($input->toCommand()); + $cacheId = sprintf( + "page.torrentio.tvshows.%s.%s.%s.%s", + $input->tmdbId, + $input->imdbId, + $input->season, + $input->episode, + ); - return $this->render('torrentio/tvshows.html.twig', [ - 'results' => $results, - ]); + return $cache->get($cacheId, function (ItemInterface $item) use ($input) { + $item->expiresAt(new \DateTimeImmutable("today 11:59 pm")); + $results = $this->getTvShowOptionsHandler->handle($input->toCommand()); + return $this->render('torrentio/tvshows.html.twig', [ + 'results' => $results, + ]); + }); } }