diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index e404719524..89c0e039d9 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -95,7 +95,7 @@ class Cache ]); $this->loadResults?->add(1, [ 'adapter' => $adapterName, - 'result' => $result ? 'hit' : 'miss', + 'result' => $result === false ? 'miss' : 'hit', ]); return $result; diff --git a/tests/Cache/Unit/LoadResultsTelemetryTest.php b/tests/Cache/Unit/LoadResultsTelemetryTest.php index 3921565a3c..2b89bfe3b5 100644 --- a/tests/Cache/Unit/LoadResultsTelemetryTest.php +++ b/tests/Cache/Unit/LoadResultsTelemetryTest.php @@ -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> $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> $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']); + } }