Skip to content

[Cache] fix cache data collector on late collect #59841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix cache data collector on late collect
  • Loading branch information
dcmbrs authored and nicolas-grekas committed Feb 26, 2025
commit 4b8bb5cc40d9fd64d2faae9de3fe43c8a649164f
20 changes: 10 additions & 10 deletions src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ public function addInstance(string $name, TraceableAdapter $instance): void

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
}

$this->data['instances']['statistics'] = $this->calculateStatistics();
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
$this->lateCollect();
}

public function reset(): void
Expand All @@ -59,7 +51,15 @@ public function reset(): void

public function lateCollect(): void
{
$this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
}

$this->data['instances']['statistics'] = $this->calculateStatistics();
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
}

public function getName(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public function testCollectBeforeEnd()
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
}

public function testLateCollect()
{
$adapter = new TraceableAdapter(new NullAdapter());

$collector = new CacheDataCollector();
$collector->addInstance(self::INSTANCE_NAME, $adapter);

$adapter->get('foo', function () use ($collector) {
$collector->lateCollect();

return 123;
});

$stats = $collector->getStatistics();
$this->assertGreaterThan(0, $stats[self::INSTANCE_NAME]['time']);
$this->assertEquals($stats[self::INSTANCE_NAME]['hits'], 0, 'hits');
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
$this->assertEquals($stats[self::INSTANCE_NAME]['calls'], 1, 'calls');
}

private function getCacheDataCollectorStatisticsFromEvents(array $traceableAdapterEvents)
{
$traceableAdapterMock = $this->createMock(TraceableAdapter::class);
Expand Down
Loading