Skip to content

Commit 3e6c761

Browse files
authored
Make sure all types of cache supports prefix and tagging (#71)
* Make sure all types of cache supports prefix and tagging * Make sure we do not use old tagging * cs * bugfix
1 parent 0f341bf commit 3e6c761

File tree

8 files changed

+67
-5
lines changed

8 files changed

+67
-5
lines changed

src/Cache/Recording/Factory.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\CacheBundle\Cache\Recording;
1313

1414
use Cache\Hierarchy\HierarchicalPoolInterface;
15+
use Cache\TagInterop\TaggableCacheItemPoolInterface;
1516
use Psr\Cache\CacheItemPoolInterface;
1617
use Psr\Log\LoggerInterface;
1718

src/DependencyInjection/CacheExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cache\Bridge\Doctrine\DoctrineCacheBridge;
1515
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
1616
use Cache\CacheBundle\Factory\DoctrineBridgeFactory;
17+
use Cache\CacheBundle\Factory\RouterFactory;
1718
use Cache\CacheBundle\Factory\SessionHandlerFactory;
1819
use Cache\CacheBundle\Factory\ValidationFactory;
1920
use Cache\CacheBundle\Routing\CachingRouter;
@@ -152,6 +153,7 @@ private function registerServices(ContainerBuilder $container, $config)
152153

153154
if ($config['router']['enabled']) {
154155
$container->register('cache.service.router', CachingRouter::class)
156+
->setFactory([RouterFactory::class, 'get'])
155157
->setDecoratedService('router', null, 10)
156158
->addArgument(new Reference($config['router']['service_id']))
157159
->addArgument(new Reference('cache.service.router.inner'))

src/DependencyInjection/Configuration.php

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ private function addSerializerSection()
8484
->children()
8585
->scalarNode('service_id')->isRequired()->end()
8686
->booleanNode('use_tagging')->defaultTrue()->end()
87+
->scalarNode('prefix')->defaultValue('')->end()
8788
->end();
8889

8990
return $node;
@@ -105,6 +106,7 @@ private function addValidationSection()
105106
->children()
106107
->scalarNode('service_id')->isRequired()->end()
107108
->booleanNode('use_tagging')->defaultTrue()->end()
109+
->scalarNode('prefix')->defaultValue('')->end()
108110
->end();
109111

110112
return $node;
@@ -126,6 +128,7 @@ private function addAnnotationSection()
126128
->children()
127129
->scalarNode('service_id')->isRequired()->end()
128130
->booleanNode('use_tagging')->defaultTrue()->end()
131+
->scalarNode('prefix')->defaultValue('')->end()
129132
->end();
130133

131134
return $node;
@@ -227,6 +230,8 @@ private function addRouterSection()
227230
->scalarNode('service_id')
228231
->isRequired()
229232
->end()
233+
->booleanNode('use_tagging')->defaultTrue()->end()
234+
->scalarNode('prefix')->defaultValue('')->end()
230235
->end();
231236

232237
return $node;

src/Factory/DoctrineBridgeFactory.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Cache\Bridge\Doctrine\DoctrineCacheBridge;
1515
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
16+
use Cache\Prefixed\PrefixedCachePool;
1617
use Cache\Taggable\TaggablePSR6PoolAdapter;
1718
use Psr\Cache\CacheItemPoolInterface;
1819

@@ -28,12 +29,16 @@ class DoctrineBridgeFactory
2829
*
2930
* @return DoctrineCacheBridge
3031
*/
31-
public static function get(CacheItemPoolInterface $pool, $config, array $tags)
32+
public static function get(CacheItemPoolInterface $pool, array $config, array $tags)
3233
{
3334
if ($config['use_tagging']) {
3435
$pool = new FixedTaggingCachePool(TaggablePSR6PoolAdapter::makeTaggable($pool), $tags);
3536
}
3637

38+
if (!empty($config['prefix'])) {
39+
$pool = new PrefixedCachePool($pool, $config['prefix']);
40+
}
41+
3742
return new DoctrineCacheBridge($pool);
3843
}
3944
}

src/Factory/RouterFactory.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[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\Factory;
13+
14+
use Cache\CacheBundle\Routing\CachingRouter;
15+
use Cache\Prefixed\PrefixedCachePool;
16+
use Cache\Taggable\TaggablePSR6PoolAdapter;
17+
use Psr\Cache\CacheItemPoolInterface;
18+
use Symfony\Component\Routing\RouterInterface;
19+
20+
/**
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
class RouterFactory
24+
{
25+
/**
26+
* @param CacheItemPoolInterface $pool
27+
* @param RouterInterface $router
28+
* @param array $config
29+
*
30+
* @return CachingRouter
31+
*/
32+
public static function get(CacheItemPoolInterface $pool, RouterInterface $router, array $config)
33+
{
34+
if ($config['use_tagging']) {
35+
$pool = TaggablePSR6PoolAdapter::makeTaggable($pool);
36+
}
37+
38+
if (!empty($config['prefix'])) {
39+
$pool = new PrefixedCachePool($pool, $config['prefix']);
40+
}
41+
42+
return new CachingRouter($pool, $router, $config);
43+
}
44+
}

src/Factory/SessionHandlerFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SessionHandlerFactory
2727
*
2828
* @return Psr6SessionHandler
2929
*/
30-
public static function get(CacheItemPoolInterface $pool, $config)
30+
public static function get(CacheItemPoolInterface $pool, array $config)
3131
{
3232
if ($config['use_tagging']) {
3333
$pool = new FixedTaggingCachePool(TaggablePSR6PoolAdapter::makeTaggable($pool), ['session']);

src/Factory/ValidationFactory.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Cache\CacheBundle\Bridge\SymfonyValidatorBridge;
1515
use Cache\CacheBundle\Cache\FixedTaggingCachePool;
16+
use Cache\Prefixed\PrefixedCachePool;
1617
use Cache\Taggable\TaggablePSR6PoolAdapter;
1718
use Psr\Cache\CacheItemPoolInterface;
1819

@@ -27,12 +28,16 @@ class ValidationFactory
2728
*
2829
* @return SymfonyValidatorBridge
2930
*/
30-
public static function get(CacheItemPoolInterface $pool, $config)
31+
public static function get(CacheItemPoolInterface $pool, array $config)
3132
{
3233
if ($config['use_tagging']) {
3334
$pool = new FixedTaggingCachePool(TaggablePSR6PoolAdapter::makeTaggable($pool), ['validation']);
3435
}
3536

37+
if (!empty($config['prefix'])) {
38+
$pool = new PrefixedCachePool($pool, $config['prefix']);
39+
}
40+
3641
return new SymfonyValidatorBridge($pool);
3742
}
3843
}

src/Routing/CachingRouter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Cache\CacheBundle\Routing;
1313

1414
use Cache\CacheBundle\KeyNormalizer;
15-
use Cache\Taggable\TaggableItemInterface;
15+
use Cache\TagInterop\TaggableCacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
1717
use Symfony\Component\Routing\RequestContext;
1818
use Symfony\Component\Routing\RouterInterface;
@@ -166,7 +166,7 @@ private function getCacheItemFromKey($key, $tag)
166166
{
167167
$item = $this->cache->getItem(KeyNormalizer::noInvalid($key));
168168

169-
if ($item instanceof TaggableItemInterface) {
169+
if ($item instanceof TaggableCacheItemInterface) {
170170
$item->setTags(['router', $tag]);
171171
}
172172

0 commit comments

Comments
 (0)