fix: treat only false as cache miss, not all falsy values

Strict === false check so cached null/0/''/[] count as hits.
Adapters use false as the miss sentinel; falsy data is still data.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Opus 4.7 committed 2026-05-19 15:16:25 +01:00
1 parent 6e84e3c8a5
commit 393210b49f
2 files changed
+45 -1

No files matched your search

+1 -1
View File
@@ -95,7 +95,7 @@ class Cache
]);
$this->loadResults?->add(1, [
'adapter' => $adapterName,
'result' => $result ? 'hit' : 'miss',
'result' => $result === false ? 'miss' : 'hit',
]);
return $result;
@@ -63,4 +63,48 @@ class LoadResultsTelemetryTest extends TestCase
$results = \array_column($captured, 'result');
$this->assertSame(['miss', 'hit'], $results);
}
public function testNullReturnIsTreatedAsHit(): void
{
$captured = [];
$adapter = new class extends Memory {
public function load(string $key, int $ttl, string $hash = ''): mixed
{
return null;
}
};
$cache = new Cache($adapter);
$telemetry = new class ($captured) extends TestTelemetry {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured) {}
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): \Utopia\Telemetry\Counter
{
if ($name !== 'cache.load.total') {
return parent::createCounter($name, $unit, $description, $advisory);
}
$captured = &$this->captured;
return $this->counters[$name] = new class ($captured) extends \Utopia\Telemetry\Counter {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured) {}
public function add(float|int $amount, iterable $attributes = []): void
{
$this->captured[] = \is_array($attributes) ? $attributes : \iterator_to_array($attributes);
}
};
}
};
$cache->setTelemetry($telemetry);
$cache->load('any', 60);
$this->assertSame('hit', $captured[0]['result']);
}
}