cache = $cacheFactory->createDistributed('openmetrics'); } /** * Number of seconds to keep the results */ abstract public function getTTL(): int; /** * Actually gather the metrics * * @see metrics */ abstract public function gatherMetrics(): Generator; #[Override] public function metrics(): Generator { $cacheKey = static::class; if ($data = $this->cache->get($cacheKey)) { yield from unserialize( $data, [ 'allowed_classes' => [Metric::class, MetricValue::class], ], ); return; } $data = []; foreach ($this->gatherMetrics() as $metric) { yield $metric; $data[] = $metric; } $this->cache->set($cacheKey, serialize($data), $this->getTTL()); } }