Skip to content

Add new cache events on CacheCollector #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 18 additions & 10 deletions src/DataCollector/CacheCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DataFormatter\HasDataFormatter;
use Illuminate\Cache\Events\CacheEvent;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\KeyForgotten;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Cache\Events\{
CacheFlushed,
CacheFlushFailed,
CacheHit,
CacheMissed,
KeyForgetFailed,
KeyForgotten,
KeyWriteFailed,
KeyWritten,
};
use Illuminate\Events\Dispatcher;

class CacheCollector extends TimeDataCollector
Expand All @@ -22,8 +27,12 @@ class CacheCollector extends TimeDataCollector
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',
];

public function __construct($requestStartTime, $collectValues)
Expand All @@ -33,7 +42,7 @@ public function __construct($requestStartTime, $collectValues)
$this->collectValues = $collectValues;
}

public function onCacheEvent(CacheEvent $event)
public function onCacheEvent($event)
{
$class = get_class($event);
$params = get_object_vars($event);
Expand All @@ -53,21 +62,20 @@ public function onCacheEvent(CacheEvent $event)
}


if (!empty($params['key']) && in_array($label, ['hit', 'written'])) {
if (!empty($params['key'] ?? null) && in_array($label, ['hit', 'written'])) {
$params['delete'] = route('debugbar.cache.delete', [
'key' => urlencode($params['key']),
'tags' => !empty($params['tags']) ? json_encode($params['tags']) : '',
]);
}

$time = microtime(true);
$this->addMeasure($label . "\t" . $event->key, $time, $time, $params);
$this->addMeasure($label . "\t" . ($params['key'] ?? ''), $time, $time, $params);
}


public function subscribe(Dispatcher $dispatcher)
{
foreach ($this->classMap as $eventClass => $type) {
foreach (array_keys($this->classMap) as $eventClass) {
$dispatcher->listen($eventClass, [$this, 'onCacheEvent']);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/cache/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
var m = lines[i];

if (measure.params && !$.isEmptyObject(measure.params)) {
if (measure.params.delete) {
$(m).next().find('td:contains(delete)').parent().remove();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a hack on CacheCollector

$params['delete'] = route('debugbar.cache.delete', [
'key' => urlencode($params['key']),
'tags' => !empty($params['tags']) ? json_encode($params['tags']) : '',
]);

delete is the hack, is added for the forget cache button, but it is not part of params
This code remove the <tr> with the delete hack from params table

if (measure.params.delete && measure.params.key) {
$('<a />')
.addClass(csscls('forget'))
Expand Down
Loading