RSS significant performance optimizations.

This commit is contained in:
Alexey Skobkin 2020-04-04 03:22:37 +03:00
parent 65d4713f7d
commit f311605ae3
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7

View file

@ -6,9 +6,7 @@ namespace App\Feed;
use App\Magnet\MagnetGenerator; use App\Magnet\MagnetGenerator;
use App\Magnetico\Entity\Torrent; use App\Magnetico\Entity\Torrent;
use App\Magnetico\Repository\TorrentRepository; use App\Magnetico\Repository\TorrentRepository;
use App\Pager\PagelessDoctrineORMAdapter;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Pagerfanta;
use Suin\RSSWriter\{Channel, Feed, Item}; use Suin\RSSWriter\{Channel, Feed, Item};
use Symfony\Component\Routing\{Generator\UrlGeneratorInterface, RouterInterface}; use Symfony\Component\Routing\{Generator\UrlGeneratorInterface, RouterInterface};
@ -35,20 +33,19 @@ class RssGenerator
public function generateLast(int $page): string public function generateLast(int $page): string
{ {
$qb = $this->createLastTorrentsQueryBuilder(); $qb = $this->createLastTorrentsQueryBuilder(self::PER_PAGE, $page * self::PER_PAGE);
$pager = new Pagerfanta(new PagelessDoctrineORMAdapter($qb));
$pager
->setAllowOutOfRangePages(true)
->setCurrentPage($page)
->setMaxPerPage(self::PER_PAGE)
;
$feed = $this->createFeedFromTorrents($pager->getCurrentPageResults()); $feed = $this->createFeedFromTorrents($qb->getQuery()->getResult());
return $feed->render(); return $feed->render();
} }
private function createFeedFromTorrents(\Traversable $torrents): Feed /**
* @param Torrent[] $torrents
*
* @return Feed
*/
private function createFeedFromTorrents(array $torrents): Feed
{ {
$feed = new Feed(); $feed = new Feed();
$channel = $this->createChannel(); $channel = $this->createChannel();
@ -83,11 +80,11 @@ class RssGenerator
} }
/** /**
* @param Torrent[]|\Traversable $torrents * @param Torrent[] $torrents
* *
* @return Item[] * @return Item[]
*/ */
private function createItemsFromTorrents(\Traversable $torrents): array private function createItemsFromTorrents(array $torrents): array
{ {
$items = []; $items = [];
@ -116,12 +113,15 @@ class RssGenerator
return $item; return $item;
} }
private function createLastTorrentsQueryBuilder(): QueryBuilder private function createLastTorrentsQueryBuilder(int $limit = self::PER_PAGE, int $offset = 0): QueryBuilder
{ {
$qb = $this->repo->createQueryBuilder('t'); $qb = $this->repo->createQueryBuilder('t');
$qb $qb
->select('t') ->select('t')
->orderBy('t.id', 'DESC'); ->orderBy('t.id', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset)
;
return $qb; return $qb;
} }