Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Processed Updates Service minor performance improvement
Browse files Browse the repository at this point in the history
chrispenny committed Oct 1, 2023
1 parent 0377266 commit 1e8e0fd
Showing 3 changed files with 15 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Services/CacheProcessingService.php
Original file line number Diff line number Diff line change
@@ -163,13 +163,13 @@ private function alreadyProcessed(string $className, int $id): bool
return false;
}

// We are in a "Draft" context, so we don't care whether or not the ProcessedUpdateDTO has been published or
// not. Its existence means that it has been processed
// We are in a "Draft" context, so we don't care whether the ProcessedUpdateDTO has been published or not. Its
// existence means that it has been processed
if (!$this->shouldPublishUpdates()) {
return true;
}

// We are in a "Live" context, so we need to return whether or not this ProcessedUpdateDTO has been published
// We are in a "Live" context, so we need to return whether this ProcessedUpdateDTO has been published
return $processedUpdate->isPublished();
}

21 changes: 10 additions & 11 deletions src/Services/ProcessedUpdatesService.php
Original file line number Diff line number Diff line change
@@ -24,21 +24,15 @@ public function getProcessedUpdates(): array

public function addProcessedUpdate(ProcessedUpdateDto $processedUpdate): void
{
$this->processedUpdates[] = $processedUpdate;
$key = $this->getProcessedUpdateKey($processedUpdate->getClassName(), $processedUpdate->getId());
$this->processedUpdates[$key] = $processedUpdate;
}

public function findProcessedUpdate(string $className, int $id): ?ProcessedUpdateDto
{
foreach ($this->processedUpdates as $processedUpdate) {
$classNameMatches = $processedUpdate->getClassName() === $className;
$idMatches = $processedUpdate->getId() === $id;
$key = $this->getProcessedUpdateKey($className, $id);

if ($idMatches && $classNameMatches) {
return $processedUpdate;
}
}

return null;
return $this->processedUpdates[$key] ?? null;
}

public function findOrCreateProcessedUpdate(string $className, int $id): ProcessedUpdateDto
@@ -50,9 +44,14 @@ public function findOrCreateProcessedUpdate(string $className, int $id): Process
}

$processedUpdate = new ProcessedUpdateDto($className, $id);
$this->processedUpdates[] = $processedUpdate;
$this->addProcessedUpdate($processedUpdate);

return $processedUpdate;
}

private function getProcessedUpdateKey(string $className, int $id): string
{
return sprintf('%s-%s', $className, $id);
}

}
4 changes: 2 additions & 2 deletions tests/Services/ProcessedUpdatesServiceTest.php
Original file line number Diff line number Diff line change
@@ -16,11 +16,11 @@ public function testAddProcessedUpdate(): void
$this->assertCount(0, $service->getProcessedUpdates());

$service->addProcessedUpdate(new ProcessedUpdateDto(Page::class, 99));
// There are no checks for duplication between DTOs
// Existing DTOs would be overridden if an identical record is added
$service->addProcessedUpdate(new ProcessedUpdateDto(Page::class, 98));
$service->addProcessedUpdate(new ProcessedUpdateDto(Page::class, 98));

$this->assertCount(3, $service->getProcessedUpdates());
$this->assertCount(2, $service->getProcessedUpdates());
}

public function testFindProcessedUpdate(): void

0 comments on commit 1e8e0fd

Please sign in to comment.