Skip to content

Commit 934fde8

Browse files
committed
Merge pull request #23 from Nyholm/doctrine
Make sure we tag the doctrine cache
2 parents b5067aa + 0706cb4 commit 934fde8

File tree

4 files changed

+163
-2
lines changed

4 files changed

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

src/Command/CacheFlushCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,23 @@ private function clearCacheForType($type)
7373

7474
$config = $this->getContainer()->getParameter(sprintf('cache.%s', $type));
7575

76+
if ($type === 'doctrine') {
77+
$this->doClearCache($type, $config['metadata']['service_id']);
78+
$this->doClearCache($type, $config['result']['service_id']);
79+
$this->doClearCache($type, $config['query']['service_id']);
80+
} else {
81+
$this->doClearCache($type, $config['service_id']);
82+
}
83+
}
84+
85+
/**
86+
* @param string $type
87+
* @param string $serviceId
88+
*/
89+
private function doClearCache($type, $serviceId)
90+
{
7691
/** @type CacheItemPoolInterface $service */
77-
$service = $this->getContainer()->get($config['service_id']);
92+
$service = $this->getContainer()->get($serviceId);
7893
if ($service instanceof TaggablePoolInterface) {
7994
$service->clear([$type]);
8095
} else {

src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php

Lines changed: 27 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\FixedTaggingCachePool;
1516
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718

@@ -69,7 +70,7 @@ protected function enableDoctrineSupport(array $config)
6970
// Doctrine can't talk to a PSR-6 cache, so we need a bridge
7071
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
7172
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
72-
$bridgeDef->addArgument(0, new Reference($cacheData['service_id']))
73+
$bridgeDef->addArgument(new Reference($this->getPoolReferenceForBridge($bridgeServiceId, $cacheData, $config['use_tagging'])))
7374
->setPublic(false);
7475

7576
foreach ($cacheData[$type] as $manager) {
@@ -88,6 +89,31 @@ protected function enableDoctrineSupport(array $config)
8889
}
8990
}
9091

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

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)