8000 [FrameworkBundle] Add config for Redis and APCu cache · symfony/symfony@09320b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09320b9

Browse files
[FrameworkBundle] Add config for Redis and APCu cache
1 parent 31159f3 commit 09320b9

File tree

9 files changed

+129
-26
lines changed

9 files changed

+129
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(ContainerBuilder $container)
6060
}
6161
unset($tags[0]['clearer']);
6262

63-
if (isset($tags[0]['provider']) && is_string($tags[0]['provider'])) {
63+
if (isset($tags[0]['provider'])) {
6464
$tags[0]['provider'] = new Reference($tags[0]['provider']);
6565
}
6666
$i = 0;

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,26 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
558558
->addDefaultsIfNotSet()
559559
->fixXmlConfig('pool')
560560
->children()
561+
->arrayNode('apcu')
562+
->info('APCu cache configuration')
563+
->canBeEnabled()
564+
->children()
565+
->integerNode('default_lifetime')->defaultValue(0)->end()
566+
->end()
567+
->end()
568+
->arrayNode('redis')
569+
->info('Redis cache configuration')
570+
->canBeEnabled()
571+
->children()
572+
->integerNode('default_lifetime')->defaultValue(0)->end()
573+
->scalarNode('persistent')->defaultFalse()->end()
574+
->scalarNode('host')->defaultValue('127.0.0.1')->end()
575+
->integerNode('port')->defaultValue(6379)->end()
576+
->floatNode('timeout')->defaultValue(0)->info('In seconds')->end()
577+
->integerNode('retry_interval')->info('In milliseconds')->end()
578+
->integerNode('database')->defaultValue(0)->info('Between 0 and 15')->end()
579+
->end()
580+
->end()
561581
->arrayNode('pools')
562582
->useAttributeAsKey('name')
563583
->prototype('array')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,18 +1037,60 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10371037

10381038
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
10391039
{
1040-
foreach ($config['pools'] as $name => $poolConfig) {
1041-
if (isset($poolConfig['parent'])) {
1042-
$poolDefinition = new DefinitionDecorator($poolConfig['parent']);
1043-
$poolDefinition->setAbstract(true);
1040+
if ($this->isConfigEnabled($container, $config['apcu'])) {
1041+
$container->setAlias('cache.adapter.local', 'cache.adapter.apcu');
1042+
1043+
if (0 < $config['apcu']['default_lifetime']) {
1044+
$container->getDefinition('cache.adapter.apcu')
1045+
->clearTag('cache.pool')
1046+
->addTag('cache.pool', array('default_lifetime' => $config['apcu']['default_lifetime']))
1047+
;
1048+
}
1049+
}
1050+
1051+
if ($this->isConfigEnabled($container, $config['redis'])) {
1052+
$container->setAlias('cache.adapter.shared', 'cache.adapter.redis');
1053+
1054+
$args = array(
1055+
$config['redis']['host'],
1056+
$config['redis']['port'],
1057+
$config['redis']['timeout'],
1058+
);
1059+
if (isset($config['retry_interval'])) {
1060+
$args[] = null; // reserved, should be null
1061+
$args[] = $config['redis']['retry_interval'];
1062+
}
1063+
$definition = $container->getDefinition('cache.default_redis_connection');
1064+
$definition->addMethodCall($config['redis']['persistent'] ? 'pconnect' : 'connect', $args);
1065+
1066+
if (!empty($config['redis']['database'])) {
1067+
$definition->addMethodCall('select', array($config['redis']['database']));
1068+
}
1069+
1070+
$args = array(
1071+
'provider' => 'cache.default_redis_connection',
1072+
);
1073+
if (0 < $config['redis']['default_lifetime']) {
1074+
$args['default_lifetime'] = $config['redis']['default_lifetime'];
1075+
}
1076+
$container->getDefinition('cache.adapter.redis')
1077+
->clearTag('cache.pool')
1078+
->addTag('cache.pool', $args)
1079+
;
1080+
}
1081+
1082+
foreach ($config['pools'] as $name => $pool) {
1083+
if (isset($pool['parent'])) {
1084+
$definition = new DefinitionDecorator($pool['parent']);
1085+
$definition->setAbstract(true);
10441086
} else {
1045-
$poolDefinition = new DefinitionDecorator($poolConfig['adapter']);
1046-
$poolDefinition->setPublic($poolConfig['public']);
1087+
$definition = new DefinitionDecorator($pool['adapter']);
1088+
$definition->setPublic($pool['public']);
10471089
}
1048-
unset($poolConfig['parent'], $poolConfig['adapter'], $poolConfig['public']);
1090+
unset($pool['parent'], $pool['adapter'], $pool['public']);
10491091

1050-
$poolDefinition->addTag('cache.pool', $poolConfig);
1051-
$container->setDefinition($name, $poolDefinition);
1092+
$definition->addTag('cache.pool', $pool);
1093+
$container->setDefinition($name, $definition);
10521094
}
10531095

10541096
$this->addClassesToCompile(array(

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<tag name="kernel.cache_clearer" />
1111
</service>
1212

13+
<service id="cache.default_redis_connection" class="Redis" public="false" />
14+
1315
<service id="cache.app" parent="cache.adapter.shared">
1416
<tag name="cache.pool" />
1517
</service>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ protected static function getBundleDefaultConfig()
268268
),
269269
'cache' => array(
270270
'pools' => array(),
271+
'apcu' => array(
272+
'enabled' => false,
273+
'default_lifetime' => 0,
274+
),
275+
'redis' => Array (
276+
'enabled' => false,
277+
'persistent' => false,
278+
'host' => '127.0.0.1',
279+
'port' => 6379,
280+
'timeout' => 0,
281+
'database' => 0,
282+
'default_lifetime' => 0,
283+
),
271284
),
272285
);
273286
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public function testRedisCachePools()
3636
}
3737
}
3838

39+
/**
40+
* @requires extension redis
41+
*/
42+
public function testRedisCustomCachePools()
43+
{
44+
try {
45+
$this->doTestCachePools(array('root_config' => 'redis_custom_config.yml', 'environment' => 'custom_redis_cache'), RedisAdapter::class);
46+
} catch (\PHPUnit_Framework_Error_Warning $e) {
47+
if (0 !== strpos($e->getMessage(), 'unable to connect to 127.0.0.1')) {
48+
throw $e;
49+
}
50+
$this->markTestSkipped($e->getMessage());
51+
}
52+
}
53+
3954
public function doTestCachePools($options, $adapterClass)
4055
{
4156
static::bootKernel($options);
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
imports:
22
- { resource: ../config/default.yml }
33

4-
services:
5-
cache.adapter.redis.connection:
6-
public: false
7-
class: Redis
8-
calls:
9-
- [connect, [127.0.0.1]]
10-
11-
cache.adapter.shared:
12-
abstract: true
13-
parent: cache.adapter.redis
14-
tags:
15-
- name: cache.pool
16-
provider: cache.adapter.redis.connection
17-
184
framework:
195
cache:
6+
redis: true
207
pools:
21-
cache.test:
22-
public: true
8+
cache.test: ~
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
services:
5+
cache.adapter.redis.connection:
6+
public: false
7+
class: Redis
8+
calls:
9+
- [connect, [127.0.0.1]]
10+
11+
cache.adapter.shared:
12+
abstract: true
13+
parent: cache.adapter.redis
14+
tags:
15+
- name: cache.pool
16+
provider: cache.adapter.redis.connection
17+
18+
framework:
19+
cache:
20+
pools:
21+
cache.test:
22+
public: true

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function __construct(\Redis $redisConnection, $namespace = '', $defaultLi
2525
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
2626
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
2727
}
28+
if (!$redisConnection->isConnected()) {
29+
throw new InvalidArgumentException('Redis connection is not connected.');
30+
}
2831
$this->redis = $redisConnection;
2932

3033
parent::__construct($namespace, $defaultLifetime);

0 commit comments

Comments
 (0)
0