Skip to content

Commit 1e7343b

Browse files
committed
* now only invoke ThreadRepository->getThreadsIdByChunks() once for all fids @ forums()
* reuse `new \DateInterval('P1D')` into `self::$cacheAgeSeconds` as int * set `Cache-Controller` response header with value `max-age=86400, public` @ `App\Controller\SitemapController` * optimize the query by return all fid instead only the given `$fid` param to prevent duplicate full table scan @ `App\Repository\Post\ThreadRepository->getThreadsIdByChunks()` - unused method `getOrderedForumsId()` @ `App\Repository\ForumRepository` @ be
1 parent dd8c740 commit 1e7343b

3 files changed

Lines changed: 12 additions & 22 deletions

File tree

be/src/Controller/SitemapController.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
class SitemapController extends AbstractController
1818
{
19-
public static int $maxUrls = 50000;
19+
private static int $maxUrls = 50000;
20+
private static int $cacheAgeSeconds = 86400;
2021

2122
public function __construct(
2223
private readonly CacheInterface $cache,
@@ -30,16 +31,12 @@ public function forums(): Response
3031
return $this->cache->get(
3132
'/sitemaps/forums',
3233
function (ItemInterface $item) {
33-
$item->expiresAfter(new \DateInterval('P1D'));
34-
$threadsIdKeyByFid = collect($this->forumRepository->getOrderedForumsId())
35-
->mapWithKeys(fn(int $fid) => [
36-
$fid => $this->threadRepository->getThreadsIdByChunks($fid, self::$maxUrls),
37-
])
38-
->toArray();
34+
$item->expiresAfter(self::$cacheAgeSeconds);
3935
return $this->renderXml(
4036
'sitemaps/forums.xml.twig',
41-
['threads_id_key_by_fid' => $threadsIdKeyByFid],
42-
);
37+
['threads_id_key_by_fid' => collect($this->threadRepository->getThreadsIdByChunks(self::$maxUrls))
38+
->mapToGroups(static fn(array $i) => [$i['fid'] => $i['tid']])],
39+
)->setMaxAge(self::$cacheAgeSeconds)->setPublic();
4340
},
4441
);
4542
}
@@ -53,15 +50,15 @@ public function threads(Request $request, Validator $validator, int $fid): Respo
5350
return $this->cache->get(
5451
"/sitemaps/forums/$fid/threads?cursor=$cursor",
5552
function (ItemInterface $item) use ($fid, $cursor) {
56-
$item->expiresAfter(new \DateInterval('P1D'));
53+
$item->expiresAfter(self::$cacheAgeSeconds);
5754
Helper::abortAPIIfNot(40406, $this->forumRepository->isForumExists($fid));
5855
return $this->renderXml(
5956
'sitemaps/threads.xml.twig',
6057
[
6158
'threads' => $this->threadRepository->getThreadsIdWithMaxPostedAtAfter($fid, $cursor, self::$maxUrls),
6259
'base_url_fe' => $this->getParameter('app.base_url.fe'),
6360
],
64-
);
61+
)->setMaxAge(self::$cacheAgeSeconds)->setPublic();
6562
},
6663
);
6764
}

be/src/Repository/ForumRepository.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ public function getOrderedForums(): array
1919
return $this->createQuery($dql)->getResult();
2020
}
2121

22-
public function getOrderedForumsId(): array
23-
{
24-
$dql = 'SELECT t.fid FROM App\Entity\Forum t ORDER BY t.fid';
25-
return $this->createQuery($dql)->getSingleColumnResult();
26-
}
27-
2822
public function isForumExists(int $fid): bool
2923
{
3024
$dql = 'SELECT 1 FROM App\Entity\Forum t WHERE t.fid = :fid';

be/src/Repository/Post/ThreadRepository.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,20 @@ public function getPosts(int $fid, array|\ArrayAccess $postsId): array
2929
return $this->getQueryResultWithParams($dql, ['fid' => $fid, 'tid' => $postsId]);
3030
}
3131

32-
public function getThreadsIdByChunks(int $fid, int $chunkSize): array
32+
public function getThreadsIdByChunks(int $chunkSize): array
3333
{
3434
// https://github.com/doctrine/orm/issues/3542
3535
// https://github.com/doctrine/dbal/issues/5018#issuecomment-2395177479
3636
// https://github.com/beberlei/DoctrineExtensions/pull/453
3737
$entityManager = $this->getEntityManager();
3838
$tableName = $entityManager->getClassMetadata(Thread::class)->getTableName();
3939
$statement = $entityManager->getConnection()->prepare(<<<"SQL"
40-
SELECT tid FROM (
40+
SELECT fid, tid FROM (
4141
SELECT fid, tid, ROW_NUMBER() OVER (PARTITION BY fid ORDER BY tid) rn FROM $tableName
42-
) t WHERE fid = :fid AND rn % :chunkSize = 0
42+
) t WHERE rn % :chunkSize = 0
4343
SQL);
44-
$statement->bindValue('fid', $fid);
4544
$statement->bindValue('chunkSize', $chunkSize);
46-
return $statement->executeQuery()->fetchFirstColumn();
45+
return $statement->executeQuery()->fetchAllAssociative();
4746
}
4847

4948
public function getThreadsIdWithMaxPostedAtAfter(int $fid, int $after, int $limit): array

0 commit comments

Comments
 (0)