From f311605ae3d9919880436416c272a8ffc5a00246 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sat, 4 Apr 2020 03:22:37 +0300 Subject: [PATCH] RSS significant performance optimizations. --- src/Feed/RssGenerator.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Feed/RssGenerator.php b/src/Feed/RssGenerator.php index 20d2f0a..c1f4976 100644 --- a/src/Feed/RssGenerator.php +++ b/src/Feed/RssGenerator.php @@ -6,9 +6,7 @@ namespace App\Feed; use App\Magnet\MagnetGenerator; use App\Magnetico\Entity\Torrent; use App\Magnetico\Repository\TorrentRepository; -use App\Pager\PagelessDoctrineORMAdapter; use Doctrine\ORM\QueryBuilder; -use Pagerfanta\Pagerfanta; use Suin\RSSWriter\{Channel, Feed, Item}; use Symfony\Component\Routing\{Generator\UrlGeneratorInterface, RouterInterface}; @@ -35,20 +33,19 @@ class RssGenerator public function generateLast(int $page): string { - $qb = $this->createLastTorrentsQueryBuilder(); - $pager = new Pagerfanta(new PagelessDoctrineORMAdapter($qb)); - $pager - ->setAllowOutOfRangePages(true) - ->setCurrentPage($page) - ->setMaxPerPage(self::PER_PAGE) - ; + $qb = $this->createLastTorrentsQueryBuilder(self::PER_PAGE, $page * self::PER_PAGE); - $feed = $this->createFeedFromTorrents($pager->getCurrentPageResults()); + $feed = $this->createFeedFromTorrents($qb->getQuery()->getResult()); return $feed->render(); } - private function createFeedFromTorrents(\Traversable $torrents): Feed + /** + * @param Torrent[] $torrents + * + * @return Feed + */ + private function createFeedFromTorrents(array $torrents): Feed { $feed = new Feed(); $channel = $this->createChannel(); @@ -83,11 +80,11 @@ class RssGenerator } /** - * @param Torrent[]|\Traversable $torrents + * @param Torrent[] $torrents * * @return Item[] */ - private function createItemsFromTorrents(\Traversable $torrents): array + private function createItemsFromTorrents(array $torrents): array { $items = []; @@ -116,12 +113,15 @@ class RssGenerator return $item; } - private function createLastTorrentsQueryBuilder(): QueryBuilder + private function createLastTorrentsQueryBuilder(int $limit = self::PER_PAGE, int $offset = 0): QueryBuilder { $qb = $this->repo->createQueryBuilder('t'); $qb ->select('t') - ->orderBy('t.id', 'DESC'); + ->orderBy('t.id', 'DESC') + ->setMaxResults($limit) + ->setFirstResult($offset) + ; return $qb; }