Skip to content

Commit aa8a51f

Browse files
committed
Merge pull request #19 from Nyholm/decorator
Use a decorator for the router
2 parents 7b72bd0 + 5d751e6 commit aa8a51f

File tree

10 files changed

+243
-218
lines changed

10 files changed

+243
-218
lines changed

src/Cache/LoggingCachePool.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
namespace Cache\CacheBundle\Cache;
1313

14+
use Cache\Taggable\TaggablePoolInterface;
1415
use Psr\Cache\CacheItemInterface;
1516
use Psr\Cache\CacheItemPoolInterface;
1617

1718
/**
1819
* @author Aaron Scherer <[email protected]>
1920
*/
20-
class LoggingCachePool implements CacheItemPoolInterface
21+
class LoggingCachePool implements CacheItemPoolInterface, TaggablePoolInterface
2122
{
2223
/**
2324
* @type array
@@ -39,28 +40,50 @@ public function __construct(CacheItemPoolInterface $cachePool)
3940
$this->cachePool = $cachePool;
4041
}
4142

42-
public function getItem($key)
43+
/**
44+
* @param string $name
45+
* @param array $arguments
46+
*
47+
* @return object
48+
*/
49+
private function timeCall($name, array $arguments = null)
4350
{
44-
$call = $this->timeCall(__FUNCTION__, [$key]);
51+
$start = microtime(true);
52+
$result = call_user_func_array([$this->cachePool, $name], $arguments);
53+
$time = microtime(true) - $start;
54+
55+
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');
56+
57+
return $object;
58+
}
59+
60+
public function getItem($key, array $tags = [])
61+
{
62+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
4563
$result = $call->result;
46-
$call->result = sprintf('<DATA:%s>', gettype($result));
64+
65+
if ($result->isHit()) {
66+
$call->result = sprintf('<DATA:%s>', gettype($result->get()));
67+
} else {
68+
$call->result = false;
69+
}
4770

4871
$this->calls[] = $call;
4972

5073
return $result;
5174
}
5275

53-
public function hasItem($key)
76+
public function hasItem($key, array $tags = [])
5477
{
55-
$call = $this->timeCall(__FUNCTION__, [$key]);
78+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
5679
$this->calls[] = $call;
5780

5881
return $call->result;
5982
}
6083

61-
public function deleteItem($key)
84+
public function deleteItem($key, array $tags = [])
6285
{
63-
$call = $this->timeCall(__FUNCTION__, [$key]);
86+
$call = $this->timeCall(__FUNCTION__, [$key, $tags]);
6487
$this->calls[] = $call;
6588

6689
return $call->result;
@@ -78,26 +101,9 @@ public function save(CacheItemInterface $item)
78101
return $call->result;
79102
}
80103

81-
/**
82-
* @param string $name
83-
* @param array $arguments
84-
*
85-
* @return object
86-
*/
87-
private function timeCall($name, array $arguments = null)
88-
{
89-
$start = microtime(true);
90-
$result = call_user_func_array([$this->cachePool, $name], $arguments);
91-
$time = microtime(true) - $start;
92-
93-
$object = (object) compact('name', 'arguments', 'start', 'time', 'result');
94-
95-
return $object;
96-
}
97-
98-
public function getItems(array $keys = [])
104+
public function getItems(array $keys = [], array $tags = [])
99105
{
100-
$call = $this->timeCall(__FUNCTION__, [$keys]);
106+
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
101107
$result = $call->result;
102108
$call->result = sprintf('<DATA:%s>', gettype($result));
103109

@@ -106,17 +112,17 @@ public function getItems(array $keys = [])
106112
return $result;
107113
}
108114

109-
public function clear()
115+
public function clear(array $tags = [])
110116
{
111-
$call = $this->timeCall(__FUNCTION__);
117+
$call = $this->timeCall(__FUNCTION__, [$tags]);
112118
$this->calls[] = $call;
113119

114120
return $call->result;
115121
}
116122

117-
public function deleteItems(array $keys)
123+
public function deleteItems(array $keys, array $tags = [])
118124
{
119-
$call = $this->timeCall(__FUNCTION__, [$keys]);
125+
$call = $this->timeCall(__FUNCTION__, [$keys, $tags]);
120126
$this->calls[] = $call;
121127

122128
return $call->result;

src/DataCollector/CacheDataCollector.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function collect(Request $request, Response $response, \Exception $except
5858
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
5959
$this->data = ['instances' => $empty, 'total' => $empty];
6060
foreach ($this->instances as $name => $instance) {
61-
$calls = $instance->getCalls();
62-
$this->data['instances']['calls'][$name] = $calls;
61+
$this->data['instances']['calls'][$name] = $instance->getCalls();
6362
}
63+
6464
$this->data['instances']['statistics'] = $this->calculateStatistics();
6565
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
6666
}
@@ -126,19 +126,21 @@ private function calculateStatistics()
126126
foreach ($calls as $call) {
127127
$statistics[$name]['calls'] += 1;
128128
$statistics[$name]['time'] += $call->time;
129-
if ($call->name === 'fetch') {
129+
if ($call->name === 'getItem') {
130130
$statistics[$name]['reads'] += 1;
131131
if ($call->result !== false) {
132132
$statistics[$name]['hits'] += 1;
133133
} else {
134134
$statistics[$name]['misses'] += 1;
135135
}
136-
} elseif ($call->name === 'contains' && $call->result === false) {
136+
} elseif ($call->name === 'hasItem') {
137137
$statistics[$name]['reads'] += 1;
138-
$statistics[$name]['misses'] += 1;
138+
if ($call->result === false) {
139+
$statistics[$name]['misses'] += 1;
140+
}
139141
} elseif ($call->name === 'save') {
140142
$statistics[$name]['writes'] += 1;
141-
} elseif ($call->name === 'delete') {
143+
} elseif ($call->name === 'deleteItem') {
142144
$statistics[$name]['deletes'] += 1;
143145
}
144146
}

src/DependencyInjection/CacheExtension.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ public function load(array $configs, ContainerBuilder $container)
3434
$config = $this->processConfiguration(new Configuration(), $configs);
3535

3636
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
37-
$loader->load('services.yml');
3837

38+
// Make sure config values are in the parameters
3939
foreach (['router', 'session', 'doctrine'] as $section) {
4040
if ($config[$section]['enabled']) {
4141
$container->setParameter('cache.'.$section, $config[$section]);
4242
}
4343
}
4444

4545
if ($config['router']['enabled']) {
46-
$container->getDefinition('cache.router_listener')
46+
$loader->load('router.yml');
47+
$container->getDefinition('cache.router')
48+
->setDecoratedService('router', null, 10)
4749
->replaceArgument(0, new Reference($config['router']['service_id']))
48-
->replaceArgument(1, $config['router']['ttl']);
49-
} else {
50-
$container->removeDefinition('cache.router_listener');
50+
->replaceArgument(2, $config['router']['ttl']);
5151
}
5252

53-
if (!$container->getParameter('kernel.debug')) {
54-
$container->removeDefinition('data_collector.cache');
53+
if ($container->getParameter('kernel.debug')) {
54+
$loader->load('data-collector.yml');
5555
}
5656
}
5757

src/DependencyInjection/Compiler/DataCollectorCompilerPass.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,17 @@ class DataCollectorCompilerPass extends BaseCompilerPass
2727
*/
2828
protected function prepare()
2929
{
30-
$collectorDefinition = $this->container->getDefinition('data_collector.cache');
30+
$collectorDefinition = $this->container->getDefinition('cache.data_collector');
3131
$serviceIds = $this->container->findTaggedServiceIds('cache.provider');
3232

3333
foreach (array_keys($serviceIds) as $id) {
3434

35-
// Duplicating definition to $originalServiceId.logged
36-
$this->container->setDefinition($id.'.logged', $this->container->findDefinition($id));
37-
3835
// Creating a LoggingCachePool instance, and passing it the new definition from above
3936
$def = $this->container->register($id.'.logger', LoggingCachePool::class);
40-
$def->addArgument(new Reference($id.'.logged'));
37+
$def->addArgument(new Reference($id.'.logger.inner'))
38+
->setDecoratedService($id, null, 10);
4139

42-
// Overwrite the original service id with the new LoggingCachePool instance
43-
$this->container->setAlias($id, $id.'.logger');
40+
// Tell the collector to add the new logger
4441
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id.'.logger')]);
4542
}
4643
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
cache.data_collector:
3+
class: Cache\CacheBundle\DataCollector\CacheDataCollector
4+
tags:
5+
- { name: data_collector, template: 'CacheBundle:Collector:cache.html.twig', id: 'cache' }

src/Resources/config/router.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
3+
cache.router:
4+
class: Cache\CacheBundle\Routing\CachingRouter
5+
arguments: [~, "@cache.router.inner", ~]
6+
7+

src/Resources/config/services.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)