Skip to content

Commit

Permalink
chunk identifiers to prevent statement with too many placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
forxec committed Mar 19, 2024
1 parent d8cfabc commit 984ac27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Classes/Command/CleanBoostQueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function configure(): void
/**
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$rows = $this->queueRepository->findError();
foreach ($rows as $row) {
Expand Down
47 changes: 24 additions & 23 deletions Classes/Domain/Repository/CacheRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function findExpiredIdentifiers(): array
)
->groupBy('identifier')
->executeQuery()
->fetchFirstColumn()
;
->fetchFirstColumn();
return $cacheIdentifiers;
}

Expand All @@ -44,8 +43,7 @@ public function findAllIdentifiers(): array
->from($this->getTableName())
->groupBy('identifier')
->executeQuery()
->fetchFirstColumn()
;
->fetchFirstColumn();
return $cacheIdentifiers;
}

Expand Down Expand Up @@ -73,28 +71,31 @@ public function findUrlsByIdentifiers(array $identifiers): array
return [];
}

$queryBuilder = $this->createQuery();
foreach ($identifiers as &$identifier) {
$identifier = $queryBuilder->createNamedParameter($identifier);
}
unset($identifier);

$result = $queryBuilder->select('*')
->from($this->getTableName())
->where(
$queryBuilder->expr()->in('identifier', $identifiers),
)
->execute();

$cacheIdentifiers = [];
while ($row = $result->fetchAssociative()) {
$content = unserialize($row['content'], ['allowed_classes' => false]);
$url = $content['url'] ?? '';
if (!$url) {
continue;

foreach (array_chunk($identifiers, 1000) as $chunk) {
$queryBuilder = $this->createQuery();
foreach ($chunk as &$identifier) {
$identifier = $queryBuilder->createNamedParameter($identifier);
}
unset($identifier);

$result = $queryBuilder->select('*')
->from($this->getTableName())
->where(
$queryBuilder->expr()->in('identifier', $chunk),
)
->execute();

while ($row = $result->fetchAssociative()) {
$content = unserialize($row['content'], ['allowed_classes' => false]);
$url = $content['url'] ?? '';
if (!$url) {
continue;
}

$cacheIdentifiers[$row['identifier']] = $url;
$cacheIdentifiers[$row['identifier']] = $url;
}
}

return $cacheIdentifiers;
Expand Down

0 comments on commit 984ac27

Please sign in to comment.