Skip to content

Commit c04df6f

Browse files
committed
Setting up loggingCachePools
1 parent 7ef8f25 commit c04df6f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Cache/LoggingCachePool.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Aequasi\Cache\CacheItem;
1515
use Aequasi\Cache\CachePool;
1616
use Psr\Cache\CacheItemInterface;
17+
use Psr\Cache\CacheItemPoolInterface;
1718

1819
/**
1920
* @author Aaron Scherer <[email protected]>
@@ -25,6 +26,21 @@ class LoggingCachePool extends CachePool
2526
*/
2627
private $calls = [];
2728

29+
/**
30+
* @type CacheItemPoolInterface
31+
*/
32+
private $cachePool;
33+
34+
/**
35+
* LoggingCachePool constructor.
36+
*
37+
* @param CacheItemPoolInterface $cachePool
38+
*/
39+
public function __construct(CacheItemPoolInterface $cachePool)
40+
{
41+
$this->cachePool = $cachePool;
42+
}
43+
2844
public function getItem($key)
2945
{
3046
$call = $this->timeCall(__FUNCTION__, [$key]);
@@ -73,7 +89,7 @@ public function save(CacheItemInterface $item)
7389
private function timeCall($name, $arguments)
7490
{
7591
$start = microtime(true);
76-
$result = call_user_func_array(['parent', $name], $arguments);
92+
$result = call_user_func_array([$this->cachePool, $name], $arguments);
7793
$time = microtime(true) - $start;
7894

7995
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');

src/DependencyInjection/CacheExtension.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection;
1313

14+
use Cache\CacheBundle\Cache\LoggingCachePool;
1415
use Cache\CacheBundle\DataCollector\CacheDataCollector;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
1618
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1719

1820
/**
@@ -33,6 +35,8 @@ public function load(array $configs, ContainerBuilder $container)
3335
$config = $this->processConfiguration(new Configuration(), $configs);
3436

3537
if ($container->getParameter('kernel.debug')) {
38+
$this->transformLoggableCachePools($container);
39+
3640
$container->register('data_collector.cache', CacheDataCollector::class)
3741
->addTag('data_collector', ['template' => CacheDataCollector::TEMPLATE, 'id' => 'cache']);
3842
}
@@ -42,10 +46,23 @@ public function load(array $configs, ContainerBuilder $container)
4246
$container->setParameter($this->getAlias().'.'.$section, $config[$section]);
4347
}
4448
}
49+
4550
}
4651

4752
public function getAlias()
4853
{
4954
return 'cache';
5055
}
56+
57+
private function transformLoggableCachePools(ContainerBuilder $container)
58+
{
59+
$serviceIds = $container->findTaggedServiceIds('cache.provider');
60+
foreach (array_keys($serviceIds) as $id) {
61+
$container->setDefinition($id.'.logged', $container->findDefinition($id));
62+
$def = $container->register($id.'.logger', LoggingCachePool::class);
63+
$def->addArgument(new Reference($id));
64+
65+
$container->setAlias($id, $id.'.logger');
66+
}
67+
}
5168
}

0 commit comments

Comments
 (0)