Skip to content

Commit 41b818f

Browse files
adelfbarryvdh
authored andcommitted
add cache tab (barryvdh#678)
* add cache tab. barryvdh#634 * More universal cache events checking * cache tab: data formatter instead of serialize
1 parent e5257dc commit 41b818f

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

config/debugbar.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
'logs' => false, // Add the latest log messages
117117
'files' => false, // Show the included files
118118
'config' => false, // Display config settings
119+
'cache' => false, // Display cache events
119120
],
120121

121122
/*
@@ -153,6 +154,9 @@
153154
'logs' => [
154155
'file' => null
155156
],
157+
'cache' => [
158+
'values' => true // collect cache values
159+
],
156160
],
157161

158162
/*

src/DataCollector/CacheCollector.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
namespace Barryvdh\Debugbar\DataCollector;
3+
4+
use DebugBar\DataCollector\TimeDataCollector;
5+
use Illuminate\Events\Dispatcher;
6+
7+
class CacheCollector extends TimeDataCollector
8+
{
9+
/** @var bool */
10+
protected $collectValues;
11+
12+
/** @var array */
13+
protected $classMap = [
14+
'Illuminate\Cache\Events\CacheHit' => 'hit',
15+
'Illuminate\Cache\Events\CacheMissed' => 'missed',
16+
'Illuminate\Cache\Events\KeyWritten' => 'write',
17+
'Illuminate\Cache\Events\KeyForgotten' => 'delete',
18+
];
19+
20+
public function __construct($requestStartTime = null, $collectValues)
21+
{
22+
parent::__construct();
23+
24+
$this->collectValues = $collectValues;
25+
}
26+
27+
public function onClassEvent($event)
28+
{
29+
$class = get_class($event);
30+
if (isset($this->classMap[$class])) {
31+
$params = [];
32+
33+
if(isset($event->minutes)) {
34+
$params['minutes'] = $event->minutes;
35+
}
36+
37+
if(isset($event->value)) {
38+
if ($this->collectValues) {
39+
$params['value'] = $this->getDataFormatter()->formatVar($event->value);
40+
} else {
41+
$params['value'] = '(values collecting turned off)';
42+
}
43+
}
44+
45+
if(!empty($event->tags)) {
46+
$params['tags'] = $event->tags;
47+
}
48+
49+
$time = microtime(true);
50+
$this->addMeasure($this->classMap[$class] . ' ' . $event->key, $time, $time, $params);
51+
}
52+
}
53+
54+
public function onStringEvent($event, $payload)
55+
{
56+
$params = [];
57+
58+
if(is_array($payload)) {
59+
if (isset($payload[2])) {
60+
$params['minutes'] = $payload[2];
61+
}
62+
63+
if (isset($payload[1])) {
64+
if ($this->collectValues) {
65+
$params['value'] = $this->getDataFormatter()->formatVar($payload[1]);
66+
} else {
67+
$params['value'] = '(values collecting turned off)';
68+
}
69+
}
70+
}
71+
72+
$time = microtime(true);
73+
$this->addMeasure( str_replace('cache.', '', $event) . ' ' . (is_array($payload) ? $payload[0] : $payload),
74+
$time, $time, $params);
75+
}
76+
77+
public function subscribe(Dispatcher $events)
78+
{
79+
if (class_exists('Illuminate\Cache\Events\CacheHit')) {
80+
$events->listen('Illuminate\Cache\Events\*', [$this, 'onClassEvent']);
81+
} else {
82+
$events->listen('cache.*', [$this, 'onStringEvent']);
83+
}
84+
}
85+
86+
public function collect()
87+
{
88+
$data = parent::collect();
89+
$data['nb_measures'] = count($data['measures']);
90+
91+
return $data;
92+
}
93+
94+
public function getName()
95+
{
96+
return 'cache';
97+
}
98+
99+
public function getWidgets()
100+
{
101+
return [
102+
'cache' => [
103+
'icon' => 'lock',
104+
'widget' => 'PhpDebugBar.Widgets.TimelineWidget',
105+
'map' => 'cache',
106+
'default' => '{}',
107+
],
108+
'cache:badge' => [
109+
'map' => 'cache.nb_measures',
110+
'default' => 0,
111+
],
112+
];
113+
}
114+
}

src/LaravelDebugbar.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Barryvdh\Debugbar;
22

33
use Barryvdh\Debugbar\DataCollector\AuthCollector;
4+
use Barryvdh\Debugbar\DataCollector\CacheCollector;
45
use Barryvdh\Debugbar\DataCollector\EventCollector;
56
use Barryvdh\Debugbar\DataCollector\FilesCollector;
67
use Barryvdh\Debugbar\DataCollector\GateCollector;
@@ -462,6 +463,25 @@ function ($query, $bindings = null, $time = null, $connectionName = null) use ($
462463
}
463464
}
464465

466+
if ($this->shouldCollect('cache', false) && isset($this->app['events'])) {
467+
try {
468+
$collectValues = $this->app['config']->get('debugbar.options.cache.values', true);
469+
$startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
470+
$cacheCollector = new CacheCollector($startTime, $collectValues);
471+
$this->addCollector($cacheCollector);
472+
$this->app['events']->subscribe($cacheCollector);
473+
474+
} catch (\Exception $e) {
475+
$this->addThrowable(
476+
new Exception(
477+
'Cannot add CacheCollector to Laravel Debugbar: ' . $e->getMessage(),
478+
$e->getCode(),
479+
$e
480+
)
481+
);
482+
}
483+
}
484+
465485
$renderer = $this->getJavascriptRenderer();
466486
$renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
467487
$renderer->setBindAjaxHandlerToXHR($app['config']->get('debugbar.capture_ajax', true));

0 commit comments

Comments
 (0)