Skip to content

Commit

Permalink
cache collector
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jul 27, 2023
1 parent 829dd41 commit d4da046
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ LeKoala\DebugBar\DebugBar:
| `db_collector` | bool | Show the db tab. Defaults to true |
| `db_save_csv` | bool | Save queries to csv in the temp folder. Use ?downloadqueries to download current. Defaults to false |
| `config_collector` | bool | Show the config tab. Defaults to true |
| `cache_collector` | bool | Show the cache tab. Defaults to true |
| `partial_cache_collector` | bool | Show the partial cache tab. Defaults to true |
| `email_collector` | bool | Show the email tab. Defaults to true |
| `header_collector` | bool | Show the headers tab. Defaults to true |
Expand Down
3 changes: 3 additions & 0 deletions _config/debugbar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ LeKoala\DebugBar\DebugBar:
config_track_empty: false
db_collector: true
db_save_csv: false
cache_collector: true
partial_cache_collector: true
email_collector: true
header_collector: true
Expand All @@ -48,6 +49,8 @@ SilverStripe\Control\Director:
rules:
'__debugbar': 'LeKoala\DebugBar\DebugBarController'
SilverStripe\Core\Injector\Injector:
Symfony\Component\Cache\Psr16Cache:
class: LeKoala\DebugBar\Proxy\CacheProxy
SilverStripe\View\SSViewer:
class: LeKoala\DebugBar\Proxy\SSViewerProxy
DebugBarMiddleware:
Expand Down
76 changes: 76 additions & 0 deletions code/Collector/CacheCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace LeKoala\DebugBar\Collector;

use LeKoala\DebugBar\DebugBar;
use SilverStripe\Control\Director;
use DebugBar\DataCollector\Renderable;
use LeKoala\DebugBar\Proxy\CacheProxy;
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;

/**
* Collects data about the cache
*/
class CacheCollector extends DataCollector implements Renderable, AssetProvider
{
public function getName()
{
return 'cache';
}

public function getAssets()
{
// This depends on ConfigCollector assets
return [
'base_path' => '/' . DebugBar::moduleResource('javascript')->getRelativePath(),
'base_url' => Director::makeRelative(DebugBar::moduleResource('javascript')->getURL()),
'css' => 'config/widget.css',
'js' => 'config/widget.js'
];
}

public function collect()
{
$result = CacheProxy::getData();


$keys = [];
foreach ($result as $k => $v) {
$val = $v['value'];
if (!is_string($val)) {
$val = json_encode($val);
}
if (strlen($val) > 150) {
$val = substr($val, 0, 150) . "...";
}
if (!empty($v['ttl'])) {
$val .= " - TTL: " . $v['ttl'];
}
$keys[$k] = $val;
}

return [
'count' => count($result),
'keys' => $keys
];
}

public function getWidgets()
{
$widgets = [
'Cache' => [
'icon' => 'puzzle-piece',
'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
'map' => 'cache.keys',
'default' => '{}'
]
];
$widgets['Cache:badge'] = [
'map' => 'cache.count',
'default' => 0
];

return $widgets;
}
}
1 change: 0 additions & 1 deletion code/Collector/DatabaseCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ protected function collectData(TimeDataCollector $timeCollector = null)

// Save as CSV
$db_save_csv = DebugBar::config()->get('db_save_csv');
$db_save_csv = true;
if ($db_save_csv && !empty($queries)) {
$filename = date('Ymd_His') . '_' . count($queries) . '_' . uniqid() . '.csv';
$isOutput = false;
Expand Down
1 change: 0 additions & 1 deletion code/Collector/HeaderCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function getWidgets()
*/
public function collect()
{

$result = $this->controller->getResponse()->getHeaders();

foreach ($result as $key => &$value) {
Expand Down
8 changes: 7 additions & 1 deletion code/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
use SilverStripe\Core\Manifest\ModuleLoader;
use DebugBar\DataCollector\MessagesCollector;
use LeKoala\DebugBar\Bridge\MonologCollector;
use LeKoala\DebugBar\Bridge\SymfonyMailer\MailerEventListener;
use Symfony\Component\Mailer\MailerInterface;
use LeKoala\DebugBar\Collector\CacheCollector;
use SilverStripe\Core\Manifest\ModuleResource;
use LeKoala\DebugBar\Collector\ConfigCollector;
use LeKoala\DebugBar\Proxy\ConfigManifestProxy;
Expand All @@ -46,6 +46,7 @@
use LeKoala\DebugBar\Collector\SilverStripeCollector;
use SilverStripe\Config\Collections\DeltaConfigCollection;
use SilverStripe\Config\Collections\CachedConfigCollection;
use LeKoala\DebugBar\Bridge\SymfonyMailer\MailerEventListener;
use LeKoala\DebugBar\Bridge\SymfonyMailer\SymfonyMailerCollector;
use LeKoala\DebugBar\Bridge\SymfonyMailer\SymfonyMailerLogCollector;

Expand Down Expand Up @@ -191,6 +192,11 @@ public static function initDebugBar()
$debugbar->addCollector(new ConfigCollector);
}

// Cache
if (self::config()->cache_collector) {
$debugbar->addCollector(new CacheCollector);
}

// Partial cache
if (self::config()->partial_cache_collector) {
$debugbar->addCollector(new PartialCacheCollector);
Expand Down
24 changes: 24 additions & 0 deletions code/Proxy/CacheProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LeKoala\DebugBar\Proxy;

use Symfony\Component\Cache\Psr16Cache;

class CacheProxy extends Psr16Cache
{
protected static $data = [];

public function set($key, $value, $ttl = null): bool
{
self::$data[$key] = [
"value" => $value,
"ttl" => $ttl
];
return parent::set($key, $value, $ttl);
}

public static function getData(): array
{
return self::$data;
}
}

0 comments on commit d4da046

Please sign in to comment.