From 7ed117d19bdd0d2818c343d3c2fc825528d8c7f1 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Sun, 22 Jun 2025 23:14:45 -0500 Subject: [PATCH] feat: adds cache pool for 'page' --- config/packages/cache.yaml | 2 ++ src/Controller/TorrentioController.php | 34 ++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/config/packages/cache.yaml b/config/packages/cache.yaml index 906a520..1788fd2 100644 --- a/config/packages/cache.yaml +++ b/config/packages/cache.yaml @@ -21,3 +21,5 @@ framework: tmdb.cache: adapter: cache.app default_lifetime: 2592000 + page.cache: + adapter: cache.app diff --git a/src/Controller/TorrentioController.php b/src/Controller/TorrentioController.php index c5ea21b..2319497 100644 --- a/src/Controller/TorrentioController.php +++ b/src/Controller/TorrentioController.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; +use Symfony\Contracts\Cache\TagAwareCacheInterface; final class TorrentioController extends AbstractController { @@ -25,7 +26,7 @@ final class TorrentioController extends AbstractController ) {} #[Route('/torrentio/movies/{tmdbId}/{imdbId}', name: 'app_torrentio_movies')] - public function movieOptions(GetMovieOptionsInput $input, CacheInterface $cache): Response + public function movieOptions(GetMovieOptionsInput $input, TagAwareCacheInterface $pageCache): Response { $cacheId = sprintf( "page.torrentio.movies.%s.%s", @@ -33,17 +34,29 @@ final class TorrentioController extends AbstractController $input->imdbId ); - return $cache->get($cacheId, function (ItemInterface $item) use ($input) { - $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); - $results = $this->getMovieOptionsHandler->handle($input->toCommand()); - return $this->render('torrentio/movies.html.twig', [ - 'results' => $results, - ]); - }); + try { + return $pageCache->get($cacheId, function (ItemInterface $item) use ($input) { + $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); + $item->tag(['page', 'page.torrentio', 'page.torrentio.movies', "page.torrentio.movies.$input->tmdbId.$input->imdbId", 'torrentio', 'torrentio.movies', "torrentio.movies.$input->tmdbId.$input->imdbId"]); + $results = $this->getMovieOptionsHandler->handle($input->toCommand()); + return $this->render('torrentio/movies.html.twig', [ + 'results' => $results, + ]); + }); + } catch (TorrentioRateLimitException $exception) { + $this->broadcaster->alert('Warning', 'Torrentio has rate limited your requests. Please wait a few minutes before trying again.', 'warning'); + return $this->render('bare.html.twig', + [], + new Response('Too many requests', + Response::HTTP_TOO_MANY_REQUESTS, + ['Retry-After' => 4000] + ) + ); + } } #[Route('/torrentio/tvshows/{tmdbId}/{imdbId}/{season?}/{episode?}', name: 'app_torrentio_tvshows')] - public function tvShowOptions(GetTvShowOptionsInput $input, CacheInterface $cache): Response + public function tvShowOptions(GetTvShowOptionsInput $input, TagAwareCacheInterface $pageCache): Response { $cacheId = sprintf( "page.torrentio.tvshows.%s.%s.%s.%s", @@ -54,8 +67,9 @@ final class TorrentioController extends AbstractController ); try { - return $cache->get($cacheId, function (ItemInterface $item) use ($input) { + return $pageCache->get($cacheId, function (ItemInterface $item) use ($input) { $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); + $item->tag(['page', 'page.torrentio', 'page.torrentio.tvshows', "page.torrentio.tvshows.$input->tmdbId.$input->imdbId", "page.torrentio.tvshows.$input->tmdbId.$input->imdbId.$input->season", "page.torrentio.tvshows.$input->tmdbId.$input->imdbId.$input->season.$input->episode", 'torrentio', 'torrentio.tvshows', "torrentio.tvshows.$input->tmdbId.$input->imdbId", "torrentio.tvshows.$input->tmdbId.$input->imdbId.$input->season", "torrentio.tvshows.$input->tmdbId.$input->imdbId.$input->season.$input->episode"]); $results = $this->getTvShowOptionsHandler->handle($input->toCommand()); return $this->render('torrentio/tvshows.html.twig', [ 'results' => $results,