Skip to content

Commit ff7bcda

Browse files
committed
Make sure we tag the doctrine cache
1 parent b5067aa commit ff7bcda

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Cache\CacheBundle\Cache;
4+
5+
use Cache\Taggable\TaggablePoolInterface;
6+
use Psr\Cache\CacheItemInterface;
7+
use Psr\Cache\CacheItemPoolInterface;
8+
9+
/**
10+
* This class is a decorator for a TaggablePoolInterface. It tags everything with 'doctrine'.
11+
* Use this class with the DoctrineBridge.
12+
*
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
class DoctrineTaggingCachePool implements CacheItemPoolInterface
16+
{
17+
/**
18+
* @var CacheItemPoolInterface|TaggablePoolInterface
19+
*/
20+
private $cache;
21+
22+
/**
23+
* @param TaggablePoolInterface $cache
24+
*/
25+
public function __construct(TaggablePoolInterface $cache)
26+
{
27+
$this->cache = $cache;
28+
}
29+
30+
/**
31+
* @{@inheritdoc}
32+
*/
33+
public function getItem($key)
34+
{
35+
return $this->cache->getItem($key, ['doctrine']);
36+
}
37+
38+
/**
39+
* @{@inheritdoc}
40+
*/
41+
public function getItems(array $keys = array())
42+
{
43+
return $this->cache->getItems($keys, ['doctrine']);
44+
}
45+
46+
/**
47+
* @{@inheritdoc}
48+
*/
49+
public function hasItem($key)
50+
{
51+
return $this->cache->hasItem($key, ['doctrine']);
52+
}
53+
54+
/**
55+
* @{@inheritdoc}
56+
*/
57+
public function clear()
58+
{
59+
return $this->cache->clear(['doctrine']);
60+
}
61+
62+
/**
63+
* @{@inheritdoc}
64+
*/
65+
public function deleteItem($key)
66+
{
67+
return $this->cache->deleteItem($key, ['doctrine']);
68+
}
69+
70+
/**
71+
* @{@inheritdoc}
72+
*/
73+
public function deleteItems(array $keys)
74+
{
75+
return $this->cache->deleteItems($keys, ['doctrine']);
76+
}
77+
78+
/**
79+
* @{@inheritdoc}
80+
*/
81+
public function save(CacheItemInterface $item)
82+
{
83+
return $this->cache->save($item);
84+
}
85+
86+
/**
87+
* @{@inheritdoc}
88+
*/
89+
public function saveDeferred(CacheItemInterface $item)
90+
{
91+
return $this->cache->saveDeferred($item);
92+
}
93+
94+
/**
95+
* @{@inheritdoc}
96+
*/
97+
public function commit()
98+
{
99+
return $this->cache->commit();
100+
}
101+
}

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\CacheBundle\DependencyInjection\Compiler;
1313

1414
use Cache\Bridge\DoctrineCacheBridge;
15+
use Cache\CacheBundle\Cache\DoctrineTaggingCachePool;
1516
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718

@@ -23,6 +24,8 @@
2324
*/
2425
class DoctrineSupportCompilerPass extends BaseCompilerPass
2526
{
27+
28+
2629
/**
2730
* @throws \Exception
2831
*
@@ -69,7 +72,7 @@ protected function enableDoctrineSupport(array $config)
6972
// Doctrine can't talk to a PSR-6 cache, so we need a bridge
7073
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
7174
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
72-
$bridgeDef->addArgument(0, new Reference($cacheData['service_id']))
75+
$bridgeDef->addArgument(new Reference($this->getPoolReferenceForBridge($bridgeServiceId, $cacheData, $config['use_tagging'])))
7376
->setPublic(false);
7477

7578
foreach ($cacheData[$type] as $manager) {
@@ -88,6 +91,30 @@ protected function enableDoctrineSupport(array $config)
8891
}
8992
}
9093

94+
/**
95+
* Get a reference string for the PSR-6 cache implementation service to use with doctrine.
96+
* If we support tagging we use the DoctrineTaggingCachePool.
97+
*
98+
* @param string $bridgeServiceId
99+
* @param array $cacheData
100+
* @param bool $tagging
101+
*
102+
* @return string
103+
*/
104+
public function getPoolReferenceForBridge($bridgeServiceId, $cacheData, $tagging)
105+
{
106+
if (!$tagging) {
107+
return $cacheData['service_id'];
108+
}
109+
110+
$taggingServiceId = $bridgeServiceId.'.tagging';
111+
$taggingDef= $this->container->register($taggingServiceId, DoctrineTaggingCachePool::class);
112+
$taggingDef->addArgument(new Reference($cacheData['service_id']))
113+
->setPublic(false);
114+
115+
return $taggingServiceId;
116+
}
117+
91118
/**
92119
* Checks to see if there are ORM's or ODM's.
93120
*

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ private function addDoctrineSection()
8484
->defaultFalse()
8585
->isRequired()
8686
->end()
87+
->booleanNode('use_tagging')
88+
->defaultTrue()
89+
->end()
8790
->end();
8891

8992
$types = ['metadata', 'result', 'query'];

0 commit comments

Comments
 (0)