Skip to content

Commit 0a924d9

Browse files
committed
upgrades
1 parent 04a13ae commit 0a924d9

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed

src/DataCollector/CacheCollector.php

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use Illuminate\Cache\Events\{
88
CacheFlushed,
99
CacheFlushFailed,
10+
CacheFlushing,
1011
CacheHit,
1112
CacheMissed,
13+
ForgettingKey,
1214
KeyForgetFailed,
1315
KeyForgotten,
1416
KeyWriteFailed,
1517
KeyWritten,
18+
RetrievingKey,
19+
WritingKey,
1620
};
1721
use Illuminate\Events\Dispatcher;
1822

@@ -23,33 +27,43 @@ class CacheCollector extends TimeDataCollector
2327
/** @var bool */
2428
protected $collectValues;
2529

30+
/** @var array */
31+
protected $eventStarts = [];
32+
2633
/** @var array */
2734
protected $classMap = [
28-
CacheHit::class => 'hit',
29-
CacheMissed::class => 'missed',
30-
CacheFlushed::class => 'flushed',
31-
CacheFlushFailed::class => 'flush_failed',
32-
KeyWritten::class => 'written',
33-
KeyWriteFailed::class => 'write_failed',
34-
KeyForgotten::class => 'forgotten',
35-
KeyForgetFailed::class => 'forget_failed',
35+
CacheHit::class => ['hit', RetrievingKey::class],
36+
CacheMissed::class => ['missed', RetrievingKey::class],
37+
CacheFlushed::class => ['flushed', CacheFlushing::class],
38+
CacheFlushFailed::class => ['flush_failed', CacheFlushing::class],
39+
KeyWritten::class => ['written', WritingKey::class],
40+
KeyWriteFailed::class => ['write_failed', WritingKey::class],
41+
KeyForgotten::class => ['forgotten', ForgettingKey::class],
42+
KeyForgetFailed::class => ['forget_failed', ForgettingKey::class],
3643
];
3744

3845
public function __construct($requestStartTime, $collectValues)
3946
{
4047
parent::__construct();
4148

4249
$this->collectValues = $collectValues;
50+
$this->memoryMeasure = true;
4351
}
4452

4553
public function onCacheEvent($event)
4654
{
4755
$class = get_class($event);
4856
$params = get_object_vars($event);
49-
50-
$label = $this->classMap[$class];
57+
$label = $this->classMap[$class][0];
58+
$endTime = microtime(true);
59+
$startHashKey = $this->getEventHash($this->classMap[$class][1] ?? '', $params);
60+
$startTime = $this->eventStarts[$startHashKey] ?? $endTime;
5161

5262
if (isset($params['value'])) {
63+
$params['memoryUsage'] = strlen(serialize($params['value'])) * 8;
64+
if (is_string($params['value'])) {
65+
$params['value'] = @unserialize($params['value']) ?: $params['value'];
66+
}
5367
if ($this->collectValues) {
5468
if ($this->isHtmlVarDumperUsed()) {
5569
$params['value'] = $this->getVarDumper()->renderVar($params['value']);
@@ -61,23 +75,43 @@ public function onCacheEvent($event)
6175
}
6276
}
6377

64-
6578
if (!empty($params['key'] ?? null) && in_array($label, ['hit', 'written'])) {
6679
$params['delete'] = route('debugbar.cache.delete', [
6780
'key' => urlencode($params['key']),
6881
'tags' => !empty($params['tags']) ? json_encode($params['tags']) : '',
6982
]);
7083
}
7184

72-
$time = microtime(true);
73-
$this->addMeasure($label . "\t" . ($params['key'] ?? ''), $time, $time, $params);
85+
$this->addMeasure($label . "\t" . ($params['key'] ?? ''), $startTime, $endTime, $params);
86+
}
87+
88+
public function onStartCacheEvent($event)
89+
{
90+
$startHashKey = $this->getEventHash(get_class($event), get_object_vars($event));
91+
$this->eventStarts[$startHashKey] = microtime(true);
92+
}
93+
94+
private function getEventHash(string $class, array $params): string
95+
{
96+
unset($params['value']);
97+
98+
return $class . ':' . substr(hash('sha256', json_encode($params)), 0, 12);
7499
}
75100

76101
public function subscribe(Dispatcher $dispatcher)
77102
{
78103
foreach (array_keys($this->classMap) as $eventClass) {
79104
$dispatcher->listen($eventClass, [$this, 'onCacheEvent']);
80105
}
106+
107+
$startEvents = array_unique(array_filter(array_map(
108+
fn ($values) => $values[1] ?? null,
109+
array_values($this->classMap)
110+
)));
111+
112+
foreach ($startEvents as $eventClass) {
113+
$dispatcher->listen($eventClass, [$this, 'onStartCacheEvent']);
114+
}
81115
}
82116

83117
public function collect()

src/LaravelDebugbar.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ public function modifyResponse(Request $request, Response $response)
905905
&& !$this->isJsonRequest($request, $response)
906906
&& $response->getContent() !== false
907907
&& in_array($request->getRequestFormat(), [null, 'html'], true)
908+
&& !$this->isJsonResponse($response)
908909
) {
909910
try {
910911
$this->injectDebugbar($response);
@@ -978,6 +979,30 @@ protected function isJsonRequest(Request $request, Response $response)
978979
return false;
979980
}
980981

982+
/**
983+
* @param \Symfony\Component\HttpFoundation\Response $response
984+
* @return bool
985+
*/
986+
protected function isJsonResponse(Response $response)
987+
{
988+
if ($response->headers->get('Content-Type') == 'application/json') {
989+
return true;
990+
}
991+
992+
$content = $response->getContent();
993+
994+
if (function_exists('json_validate')) {
995+
return json_validate($content);
996+
} else if (is_string($content)) {
997+
// PHP <= 8.2 check
998+
json_decode($content, true);
999+
1000+
return json_last_error() === JSON_ERROR_NONE;
1001+
}
1002+
1003+
return false;
1004+
}
1005+
9811006
/**
9821007
* Collects the data from the collectors
9831008
*

0 commit comments

Comments
 (0)