Skip to content
Merged
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
55 changes: 42 additions & 13 deletions src/DataCollector/CacheCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use Illuminate\Cache\Events\{
CacheFlushed,
CacheFlushFailed,
CacheFlushing,
CacheHit,
CacheMissed,
ForgettingKey,
KeyForgetFailed,
KeyForgotten,
KeyWriteFailed,
KeyWritten,
RetrievingKey,
WritingKey,
};
use Illuminate\Events\Dispatcher;

Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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']),
Expand All @@ -70,14 +75,38 @@ 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)
{
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()
Expand Down
Loading