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 a19be9f
Showing 1 changed file with 24 additions and 23 deletions.
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 a19be9f

Please sign in to comment.