When Config\Doctrine::$secondLevelCacheStatistics is enabled, Doctrine
collects per-region hit / miss / put counters via its native
Doctrine\ORM\Cache\Logging\StatisticsCacheLogger. The library exposes the
logger so you can read or reset the counters.
- Backend reuse: uses your framework cache backend (file / Redis / Memcached / array) as PSR-6 pool.
- Toggle: enable via
Config\Doctrine::$secondLevelCache = true(TTL viaConfig\Cache::$ttlby default — seeconfiguration.md). - Statistics: enable counters with
Config\Doctrine::$secondLevelCacheStatistics = true.
-
Configure your preferred cache handler in
Config\Cache. -
In
Config\Doctrine:public bool $secondLevelCache = true; public bool $secondLevelCacheStatistics = true; // optional
-
Mark entities cacheable with
#[ORM\Cache]attributes — seesecond_level_cache.md.
$logger = \Config\Services::doctrine()->getSecondLevelCacheLogger();
// ?Doctrine\ORM\Cache\Logging\StatisticsCacheLogger
if ($logger === null) {
// Statistics are not enabled (Config\Doctrine::$secondLevelCacheStatistics = false)
// or the configured logger is not a StatisticsCacheLogger.
return;
}
$hits = $logger->getHitCount();
$misses = $logger->getMissCount();
$puts = $logger->getPutCount();
// Per-region:
$regionHits = $logger->getRegionHitCount('your_region_name');StatisticsCacheLogger is part of Doctrine ORM; refer to the
Doctrine ORM Cache documentation
for the full API.
To reset counters programmatically (useful at the start of a long-running worker, or in tests):
\Config\Services::doctrine()->resetSecondLevelCacheStatistics();The package also ships a CodeIgniter filter that performs this reset at the
start of every HTTP request (so the toolbar shows per-request stats); see
debug_toolbar.md for the
registration steps.
When secondLevelCacheStatistics is enabled:
- the Doctrine panel shows a compact badge
SLC:hits/misses/puts (ratio%) - inside the panel a small table lists hits, misses and puts
See debug_toolbar.md for the toolbar integration.
- Clear metadata / proxies after changing the
#[ORM\Cache]attributes of an entity to avoid stale state. - Associations require the target entity to be cacheable for join caching to work.
- If stats do not appear in the toolbar, ensure the toolbar is enabled and the Doctrine service has been instantiated at least once during the request.