diff --git a/src/DataCollector/CacheCollector.php b/src/DataCollector/CacheCollector.php index 03847e6f..05951472 100644 --- a/src/DataCollector/CacheCollector.php +++ b/src/DataCollector/CacheCollector.php @@ -7,12 +7,16 @@ use Illuminate\Cache\Events\{ CacheFlushed, CacheFlushFailed, + CacheFlushing, CacheHit, CacheMissed, + ForgettingKey, KeyForgetFailed, KeyForgotten, KeyWriteFailed, KeyWritten, + RetrievingKey, + WritingKey, }; use Illuminate\Events\Dispatcher; @@ -23,21 +27,24 @@ class CacheCollector extends TimeDataCollector /** @var bool */ protected $collectValues; + /** @var array */ + protected $eventStarts = []; + /** @var array */ protected $classMap = [ - CacheHit::class => 'hit', - CacheMissed::class => 'missed', - CacheFlushed::class => 'flushed', - CacheFlushFailed::class => 'flush_failed', - KeyWritten::class => 'written', - KeyWriteFailed::class => 'write_failed', - KeyForgotten::class => 'forgotten', - KeyForgetFailed::class => 'forget_failed', + CacheHit::class => ['hit', RetrievingKey::class], + CacheMissed::class => ['missed', RetrievingKey::class], + CacheFlushed::class => ['flushed', CacheFlushing::class], + CacheFlushFailed::class => ['flush_failed', CacheFlushing::class], + KeyWritten::class => ['written', WritingKey::class], + KeyWriteFailed::class => ['write_failed', WritingKey::class], + KeyForgotten::class => ['forgotten', ForgettingKey::class], + KeyForgetFailed::class => ['forget_failed', ForgettingKey::class], ]; public function __construct($requestStartTime, $collectValues) { - parent::__construct(); + parent::__construct($requestStartTime); $this->collectValues = $collectValues; } @@ -46,8 +53,7 @@ public function onCacheEvent($event) { $class = get_class($event); $params = get_object_vars($event); - - $label = $this->classMap[$class]; + $label = $this->classMap[$class][0]; if (isset($params['value'])) { if ($this->collectValues) { @@ -61,7 +67,6 @@ public function onCacheEvent($event) } } - if (!empty($params['key'] ?? null) && in_array($label, ['hit', 'written'])) { $params['delete'] = route('debugbar.cache.delete', [ 'key' => urlencode($params['key']), @@ -70,7 +75,22 @@ public function onCacheEvent($event) } $time = microtime(true); - $this->addMeasure($label . "\t" . ($params['key'] ?? ''), $time, $time, $params); + $startHashKey = $this->getEventHash($this->classMap[$class][1] ?? '', $params); + $startTime = $this->eventStarts[$startHashKey] ?? $time; + $this->addMeasure($label . "\t" . ($params['key'] ?? ''), $startTime, $time, $params); + } + + public function onStartCacheEvent($event) + { + $startHashKey = $this->getEventHash(get_class($event), get_object_vars($event)); + $this->eventStarts[$startHashKey] = microtime(true); + } + + private function getEventHash(string $class, array $params): string + { + unset($params['value']); + + return $class . ':' . substr(hash('sha256', json_encode($params)), 0, 12); } public function subscribe(Dispatcher $dispatcher) @@ -78,6 +98,15 @@ public function subscribe(Dispatcher $dispatcher) foreach (array_keys($this->classMap) as $eventClass) { $dispatcher->listen($eventClass, [$this, 'onCacheEvent']); } + + $startEvents = array_unique(array_filter(array_map( + fn ($values) => $values[1] ?? null, + array_values($this->classMap) + ))); + + foreach ($startEvents as $eventClass) { + $dispatcher->listen($eventClass, [$this, 'onStartCacheEvent']); + } } public function collect()