Skip to content

Commit d28ce60

Browse files
authored
Processed Updates Service minor performance improvement (#75)
* Processed Updates Service minor performance improvement * Make sure records are in DB before processing
1 parent d3ce4d6 commit d28ce60

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/Services/CacheProcessingService.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ abstract protected function shouldPublishUpdates(): bool;
2222

2323
public function processChange(DataObject $instance): void
2424
{
25+
// Can't process a record that hasn't been saved to the Database. This would only happen if a developer
26+
// specifically calls processChange() in their code. All module hooks for this method are triggered *after*
27+
// write() type events
28+
if (!$instance->isInDB()) {
29+
return;
30+
}
31+
2532
$className = $instance->getClassName();
2633

2734
// This record has already been processed in full. It is possible for multiple write() actions to be performed
@@ -163,13 +170,13 @@ private function alreadyProcessed(string $className, int $id): bool
163170
return false;
164171
}
165172

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

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

src/Services/ProcessedUpdatesService.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ public function getProcessedUpdates(): array
2424

2525
public function addProcessedUpdate(ProcessedUpdateDto $processedUpdate): void
2626
{
27-
$this->processedUpdates[] = $processedUpdate;
27+
$key = $this->getProcessedUpdateKey($processedUpdate->getClassName(), $processedUpdate->getId());
28+
$this->processedUpdates[$key] = $processedUpdate;
2829
}
2930

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

36-
if ($idMatches && $classNameMatches) {
37-
return $processedUpdate;
38-
}
39-
}
40-
41-
return null;
35+
return $this->processedUpdates[$key] ?? null;
4236
}
4337

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

5246
$processedUpdate = new ProcessedUpdateDto($className, $id);
53-
$this->processedUpdates[] = $processedUpdate;
47+
$this->addProcessedUpdate($processedUpdate);
5448

5549
return $processedUpdate;
5650
}
5751

52+
private function getProcessedUpdateKey(string $className, int $id): string
53+
{
54+
return sprintf('%s-%s', $className, $id);
55+
}
56+
5857
}

tests/Services/ProcessedUpdatesServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function testAddProcessedUpdate(): void
1616
$this->assertCount(0, $service->getProcessedUpdates());
1717

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

23-
$this->assertCount(3, $service->getProcessedUpdates());
23+
$this->assertCount(2, $service->getProcessedUpdates());
2424
}
2525

2626
public function testFindProcessedUpdate(): void

0 commit comments

Comments
 (0)