DebugBar Second-Level Cache Collector
Written on April 10th, 2019 by Cody SniderThis will add a collector to handle Doctrine’s second-level cache and display basic information on puts, hits and misses on your PHP DebugBar.
<?php
namespace Utilities\DebugBar;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
/**
* Handles the second-level cache statistics
*/
class SecondLevelCollector extends DataCollector implements Renderable
{
/**
* @var StatisticsCacheLogger
*/
protected $logger;
/**
* @param StatisticsCacheLogger $logger
*/
public function __construct(StatisticsCacheLogger $logger)
{
$this->logger = $logger;
}
/**
* @return array
*/
public function collect()
{
return [
'stats' => implode(',', [
'puts' => $this->logger->getPutCount(),
'hits' => $this->logger->getHitCount(),
'misses' => $this->logger->getMissCount(),
])
];
}
/**
* @return string
*/
public function getName()
{
return 'second_level';
}
/**
* @return array
*/
public function getWidgets()
{
return [
"second_level" => [
"icon" => "database",
"map" => "second_level.stats",
"tooltip" => 'Second-level Cache (puts, hits, misses)',
"default" => "[]"
]
];
}
}
Then instantiate the cache logger, set it to the cache config and add it to the new collector.
/** $config Doctrine\ORM\Configuration */
$cacheLogger = new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger();
$config->getSecondLevelCacheConfiguration()->setCacheLogger($cacheLogger);
$debugbar->addCollector(new SecondLevelCollector($cacheLogger));