Skip to content

Commit c369b72

Browse files
committed
Fix decorating TagAware adapters in dev
1 parent adc39a2 commit c369b72

File tree

5 files changed

+282
-176
lines changed

5 files changed

+282
-176
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
1415
use Symfony\Component\Cache\Adapter\TraceableAdapter;
16+
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
1517
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1618
use Symfony\Component\DependencyInjection\ContainerBuilder;
1719
use Symfony\Component\DependencyInjection\Reference;
@@ -34,11 +36,12 @@ public function process(ContainerBuilder $container)
3436

3537
$collectorDefinition = $container->getDefinition('data_collector.cache');
3638
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
37-
if ($container->getDefinition($id)->isAbstract()) {
39+
$definition = $container->getDefinition($id);
40+
if ($definition->isAbstract()) {
3841
continue;
3942
}
4043

41-
$container->register($id.'.recorder', TraceableAdapter::class)
44+
$container->register($id.'.recorder', is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class)
4245
->setDecoratedService($id)
4346
->addArgument(new Reference($id.'.recorder.inner'))
4447
->setPublic(false);

src/Symfony/Component/Cache/Adapter/TraceableAdapter.php

Lines changed: 2 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Cache\Adapter;
1313

14-
use Psr\Cache\CacheItemInterface;
14+
use Symfony\Component\Cache\Traits\TraceableTrait;
1515

1616
/**
1717
* An adapter that collects data about all cache calls.
@@ -22,177 +22,5 @@
2222
*/
2323
class TraceableAdapter implements AdapterInterface
2424
{
25-
private $pool;
26-
private $calls = array();
27-
28-
public function __construct(AdapterInterface $pool)
29-
{
30-
$this->pool = $pool;
31-
}
32-
33-
/**
34-
* {@inheritdoc}
35-
*/
36-
public function getItem($key)
37-
{
38-
$event = $this->start(__FUNCTION__);
39-
try {
40-
$item = $this->pool->getItem($key);
41-
} finally {
42-
$event->end = microtime(true);
43-
}
44-
if ($event->result[$key] = $item->isHit()) {
45-
++$event->hits;
46-
} else {
47-
++$event->misses;
48-
}
49-
50-
return $item;
51-
}
52-
53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function hasItem($key)
57-
{
58-
$event = $this->start(__FUNCTION__);
59-
try {
60-
return $event->result[$key] = $this->pool->hasItem($key);
61-
} finally {
62-
$event->end = microtime(true);
63-
}
64-
}
65-
66-
/**
67-
* {@inheritdoc}
68-
*/
69-
public function deleteItem($key)
70-
{
71-
$event = $this->start(__FUNCTION__);
72-
try {
73-
return $event->result[$key] = $this->pool->deleteItem($key);
74-
} finally {
75-
$event->end = microtime(true);
76-
}
77-
}
78-
79-
/**
80-
* {@inheritdoc}
81-
*/
82-
public function save(CacheItemInterface $item)
83-
{
84-
$event = $this->start(__FUNCTION__);
85-
try {
86-
return $event->result[$item->getKey()] = $this->pool->save($item);
87-
} finally {
88-
$event->end = microtime(true);
89-
}
90-
}
91-
92-
/**
93-
* {@inheritdoc}
94-
*/
95-
public function saveDeferred(CacheItemInterface $item)
96-
{
97-
$event = $this->start(__FUNCTION__);
98-
try {
99-
return $event->result[$item->getKey()] = $this->pool->saveDeferred($item);
100-
} finally {
101-
$event->end = microtime(true);
102-
}
103-
}
104-
105-
/**
106-
* {@inheritdoc}
107-
*/
108-
public function getItems(array $keys = array())
109-
{
110-
$event = $this->start(__FUNCTION__, $keys);
111-
try {
112-
$result = $this->pool->getItems($keys);
113-
} finally {
114-
$event->end = microtime(true);
115-
}
116-
$f = function () use ($result, $event) {
117-
$event->result = array();
118-
foreach ($result as $key => $item) {
119-
if ($event->result[$key] = $item->isHit()) {
120-
++$event->hits;
121-
} else {
122-
++$event->misses;
123-
}
124-
yield $key => $item;
125-
}
126-
};
127-
128-
return $f();
129-
}
130-
131-
/**
132-
* {@inheritdoc}
133-
*/
134-
public function clear()
135-
{
136-
$event = $this->start(__FUNCTION__);
137-
try {
138-
return $event->result = $this->pool->clear();
139-
} finally {
140-
$event->end = microtime(true);
141-
}
142-
}
143-
144-
/**
145-
* {@inheritdoc}
146-
*/
147-
public function deleteItems(array $keys)
148-
{
149-
$event = $this->start(__FUNCTION__);
150-
$event->result['keys'] = $keys;
151-
try {
152-
return $event->result['result'] = $this->pool->deleteItems($keys);
153-
} finally {
154-
$event->end = microtime(true);
155-
}
156-
}
157-
158-
/**
159-
* {@inheritdoc}
160-
*/
161-
public function commit()
162-
{
163-
$event = $this->start(__FUNCTION__);
164-
try {
165-
return $event->result = $this->pool->commit();
166-
} finally {
167-
$event->end = microtime(true);
168-
}
169-
}
170-
171-
public function getCalls()
172-
{
173-
try {
174-
return $this->calls;
175-
} finally {
176-
$this->calls = array();
177-
}
178-
}
179-
180-
private function start($name)
181-
{
182-
$this->calls[] = $event = new TraceableAdapterEvent();
183-
$event->name = $name;
184-
$event->start = microtime(true);
185-
186-
return $event;
187-
}
188-
}
189-
190-
class TraceableAdapterEvent
191-
{
192-
public $name;
193-
public $start;
194-
public $end;
195-
public $result;
196-
public $hits = 0;
197-
public $misses = 0;
25+
use TraceableTrait;
19826
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Adapter;
13+
14+
use Symfony\Component\Cache\Traits\TraceableTrait;
15+
16+
/**
17+
* @author Robin Chalas <robin.chalas@gmail.com>
18+
*/
19+
class TraceableTagAwareAdapter implements TagAwareAdapterInterface
20+
{
21+
use TraceableTrait;
22+
23+
public function __construct(TagAwareAdapterInterface $pool)
24+
{
25+
$this->pool = $pool;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function invalidateTags(array $tags)
32+
{
33+
$event = $this->start(__FUNCTION__);
34+
try {
35+
return $event->result = $this->pool->invalidateTags($tags);
36+
} finally {
37+
$event->end = microtime(true);
38+
}
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
15+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
16+
use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
17+
18+
class TraceableTagAwareAdapterTest extends TraceableAdapterTest
19+
{
20+
public function testInvalidateTags()
21+
{
22+
$pool = new TraceableTagAwareAdapter(new TagAwareAdapter(new FilesystemAdapter()));
23+
$pool->invalidateTags(array('foo'));
24+
$calls = $pool->getCalls();
25+
$this->assertCount(1, $calls);
26+
27+
$call = $calls[0];
28+
$this->assertSame('invalidateTags', $call->name);
29+
$this->assertSame(0, $call->hits);
30+
$this->assertSame(0, $call->misses);
31+
$this->assertNotEmpty($call->start);
32+
$this->assertNotEmpty($call->end);
33+
}
34+
}

0 commit comments

Comments
 (0)