Skip to content
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

Create CacheCollector to display Cache stats in the debug bar #3396

Open
wants to merge 1 commit into
base: develop
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions core/classes/Core/Cache.php
Expand Up @@ -65,14 +65,20 @@ public function isCached(string $key): bool {
if (isset($cachedData[$key])) {
$entry = $cachedData[$key];
if ($entry && $this->_checkExpired($entry['time'], $entry['expire'])) {
return false;
$is_cached = false;
} else {
$is_cached = isset($cachedData[$key]['data']);
}

return isset($cachedData[$key]['data']);
}
}

return false;
if (!isset($is_cached)) {
$is_cached = false;
}

CacheCollector::getInstance()->recordCheck($key, $is_cached);
Copy link
Member

Choose a reason for hiding this comment

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

Can we be sure that, if we call this method, we won't see any errors in production websites (i.e. not using developer dependencies) because CacheCollector imports DebugBar


return $is_cached;
}

/**
Expand Down Expand Up @@ -220,6 +226,7 @@ public function store(string $key, $data, int $expiration = 0): Cache {
}
$cacheData = json_encode($dataArray);
file_put_contents($this->getCacheDir(), $cacheData);
CacheCollector::getInstance()->recordSet($key, $data, $expiration);
return $this;
}

Expand All @@ -236,17 +243,22 @@ public function retrieve(string $key, bool $timestamp = false) {
$type = $timestamp ? 'time' : 'data';

if (!isset($cachedData[$key][$type])) {
CacheCollector::getInstance()->recordMiss($key);
return null;
}

if (!$timestamp) {
$entry = $cachedData[$key];
if ($entry && $this->_checkExpired($entry['time'], $entry['expire'])) {
CacheCollector::getInstance()->recordMiss($key);
return null;
}
}

return unserialize($cachedData[$key][$type]);
$data = unserialize($cachedData[$key][$type]);
CacheCollector::getInstance()->recordHit("{$this->_cachename}:{$key}", $data);

return $data;
}

/**
Expand Down
92 changes: 92 additions & 0 deletions core/classes/Misc/CacheCollector.php
@@ -0,0 +1,92 @@
<?php

use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;

class CacheCollector extends DataCollector implements Renderable, AssetProvider {

private array $_cache_calls = [];
private static CacheCollector $_instance;

public static function getInstance(): CacheCollector {
return self::$_instance ??= new self();
}

public function recordCheck(string $key, bool $is_cached): void {
$this->recordEvent('check', [
'key' => $key,
'is_cached' => $is_cached,
]);
}

public function recordHit(string $key, $value): void {
$this->recordEvent('hit', [
'key' => $key,
'value' => $value,
]);
}

public function recordMiss(string $key): void {
$this->recordEvent('miss', [
'key' => $key,
]);
}

public function recordSet(string $key, $value, int $ttl): void {
$this->recordEvent('set', [
'key' => $key,
'value' => $value,
'ttl' => $ttl,
]);
}

public function collect(): array {
$events = [];

foreach ($this->_cache_calls as $i => $event) {
++$i;
['event' => $event, 'params' => $params] = $event;

$events["{$event} #{$i}"] = [
$this->getVarDumper()->renderVar($params),
];
}

return [
'count' => count($events),
'vars' => $events,
];
}

private function recordEvent(string $event, array $params): array {
return $this->_cache_calls[] = [
'event' => $event,
'params' => $params,
];
}

public function getName(): string {
return 'cache';
}

public function getAssets(): array {
return $this->getVarDumper()->getAssets();
}

public function getWidgets(): array
{
return [
'cache' => [
'icon' => 'tags',
'widget' => 'PhpDebugBar.Widgets.HtmlVariableListWidget',
'map' => 'cache.vars',
'default' => '{}',
],
'cache:badge' => [
'map' => 'cache.count',
'default' => 0,
],
];
}
}
1 change: 1 addition & 0 deletions core/classes/Misc/DebugBarHelper.php
Expand Up @@ -34,6 +34,7 @@ public function enable(Smarty $smarty): void {
$debugbar->addCollector($requestCollector);

$debugbar->addCollector(EventCollector::getInstance());
$debugbar->addCollector(CacheCollector::getInstance());

$configCollector = new ConfigCollector();
$configCollector->useHtmlVarDumper();
Expand Down