Skip to content

Add cache.adapter.redis_tag_aware to use RedisCacheAwareAdapter #36596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Deprecated the public `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`,
`cache_clearer`, `filesystem` and `validator` services to private.
* Added `TemplateAwareDataCollectorInterface` and `AbstractDataCollector` to simplify custom data collector creation and leverage autoconfiguration
* Add `cache.adapter.redis_tag_aware` tag to use `RedisCacheAwareAdapter`

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,11 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
foreach ($config['pools'] as $name => $pool) {
$pool['adapters'] = $pool['adapters'] ?: ['cache.app'];

$isRedisTagAware = ['cache.adapter.redis_tag_aware'] === $pool['adapters'];
foreach ($pool['adapters'] as $provider => $adapter) {
if ($config['pools'][$adapter]['tags'] ?? false) {
if (($config['pools'][$adapter]['adapters'] ?? null) === ['cache.adapter.redis_tag_aware']) {
$isRedisTagAware = true;
} elseif ($config['pools'][$adapter]['tags'] ?? false) {
$pool['adapters'][$provider] = $adapter = '.'.$adapter.'.inner';
}
}
Expand All @@ -1877,7 +1880,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
$pool['reset'] = 'reset';
}

if ($pool['tags']) {
if ($isRedisTagAware) {
$tagAwareId = $name;
$container->setAlias('.'.$name.'.inner', $name);
} elseif ($pool['tags']) {
if (true !== $pool['tags'] && ($config['pools'][$pool['tags']]['tags'] ?? false)) {
$pool['tags'] = '.'.$pool['tags'].'.inner';
}
Expand All @@ -1887,22 +1893,20 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
->setPublic($pool['public'])
;

$pool['name'] = $name;
$pool['name'] = $tagAwareId = $name;
$pool['public'] = false;
$name = '.'.$name.'.inner';

if (!\in_array($pool['name'], ['cache.app', 'cache.system'], true)) {
$container->registerAliasForArgument($pool['name'], TagAwareCacheInterface::class);
$container->registerAliasForArgument($name, CacheInterface::class, $pool['name']);
$container->registerAliasForArgument($name, CacheItemPoolInterface::class, $pool['name']);
}
} elseif (!\in_array($name, ['cache.app', 'cache.system'], true)) {
$container->register('.'.$name.'.taggable', TagAwareAdapter::class)
$tagAwareId = '.'.$name.'.taggable';
$container->register($tagAwareId, TagAwareAdapter::class)
->addArgument(new Reference($name))
;
$container->registerAliasForArgument('.'.$name.'.taggable', TagAwareCacheInterface::class, $name);
$container->registerAliasForArgument($name, CacheInterface::class);
$container->registerAliasForArgument($name, CacheItemPoolInterface::class);
}

if (!\in_array($name, ['cache.app', 'cache.system'], true)) {
$container->registerAliasForArgument($tagAwareId, TagAwareCacheInterface::class, $pool['name'] ?? $name);
$container->registerAliasForArgument($name, CacheInterface::class, $pool['name'] ?? $name);
$container->registerAliasForArgument($name, CacheItemPoolInterface::class, $pool['name'] ?? $name);
}

$definition->setPublic($pool['public']);
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Cache\Adapter\PdoAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
Expand Down Expand Up @@ -148,6 +149,22 @@
])
->tag('monolog.logger', ['channel' => 'cache'])

->set('cache.adapter.redis_tag_aware', RedisTagAwareAdapter::class)
->abstract()
->args([
abstract_arg('Redis connection service'),
'', // namespace
0, // default lifetime
service('cache.default_marshaller')->ignoreOnInvalid(),
])
->call('setLogger', [service('logger')->ignoreOnInvalid()])
->tag('cache.pool', [
'provider' => 'cache.default_redis_provider',
'clearer' => 'cache.default_clearer',
'reset' => 'reset',
])
->tag('monolog.logger', ['channel' => 'cache'])

->set('cache.adapter.memcached', MemcachedAdapter::class)
->abstract()
->args([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
'redis://foo' => 'cache.adapter.redis',
],
],
'cache.redis_tag_aware.foo' => [
'adapter' => 'cache.adapter.redis_tag_aware',
],
'cache.redis_tag_aware.foo2' => [
'tags' => true,
'adapter' => 'cache.adapter.redis_tag_aware',
],
'cache.redis_tag_aware.bar' => [
'adapter' => 'cache.redis_tag_aware.foo',
],
'cache.redis_tag_aware.bar2' => [
'tags' => true,
'adapter' => 'cache.redis_tag_aware.foo',
],
'cache.redis_tag_aware.baz' => [
'adapter' => 'cache.redis_tag_aware.foo2',
],
'cache.redis_tag_aware.baz2' => [
'tags' => true,
'adapter' => 'cache.redis_tag_aware.foo2',
],
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<framework:adapter name="cache.adapter.filesystem" />
<framework:adapter name="cache.adapter.redis" provider="redis://foo" />
</framework:pool>
<framework:pool name="cache.redis_tag_aware.foo" adapter="cache.adapter.redis_tag_aware" />
<framework:pool name="cache.redis_tag_aware.foo2" tags="true" adapter="cache.adapter.redis_tag_aware" />
<framework:pool name="cache.redis_tag_aware.bar" adapter="cache.redis_tag_aware.foo" />
<framework:pool name="cache.redis_tag_aware.bar2" tags="true" adapter="cache.redis_tag_aware.foo" />
<framework:pool name="cache.redis_tag_aware.baz" adapter="cache.redis_tag_aware.foo2" />
<framework:pool name="cache.redis_tag_aware.baz2" tags="true" adapter="cache.redis_tag_aware.foo2" />
</framework:cache>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ framework:
- cache.adapter.array
- cache.adapter.filesystem
- {name: cache.adapter.redis, provider: 'redis://foo'}
cache.redis_tag_aware.foo:
adapter: cache.adapter.redis_tag_aware
cache.redis_tag_aware.foo2:
tags: true
adapter: cache.adapter.redis_tag_aware
cache.redis_tag_aware.bar:
adapter: cache.redis_tag_aware.foo
cache.redis_tag_aware.bar2:
tags: true
adapter: cache.redis_tag_aware.foo
cache.redis_tag_aware.baz:
adapter: cache.redis_tag_aware.foo2
cache.redis_tag_aware.baz2:
tags: true
adapter: cache.redis_tag_aware.foo2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;

use Doctrine\Common\Annotations\Annotation;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
Expand All @@ -27,6 +28,7 @@
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand Down Expand Up @@ -56,6 +58,8 @@
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Workflow;
use Symfony\Component\Workflow\WorkflowEvents;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -1316,6 +1320,41 @@ public function testCachePoolServices()
$this->assertEquals($expected, $chain->getArguments());
}

public function testRedisTagAwareAdapter(): void
{
$container = $this->createContainerFromFile('cache', [], true);

$aliasesForArguments = [];
$argNames = [
'cacheRedisTagAwareFoo',
'cacheRedisTagAwareFoo2',
'cacheRedisTagAwareBar',
'cacheRedisTagAwareBar2',
'cacheRedisTagAwareBaz',
'cacheRedisTagAwareBaz2',
];
foreach ($argNames as $argumentName) {
$aliasesForArguments[] = sprintf('%s $%s', TagAwareCacheInterface::class, $argumentName);
$aliasesForArguments[] = sprintf('%s $%s', CacheInterface::class, $argumentName);
$aliasesForArguments[] = sprintf('%s $%s', CacheItemPoolInterface::class, $argumentName);
}

foreach ($aliasesForArguments as $aliasForArgumentStr) {
$aliasForArgument = $container->getAlias($aliasForArgumentStr);
$this->assertNotNull($aliasForArgument, sprintf("No alias found for '%s'", $aliasForArgumentStr));

$def = $container->getDefinition((string) $aliasForArgument);
$this->assertInstanceOf(ChildDefinition::class, $def, sprintf("No definition found for '%s'", $aliasForArgumentStr));

$defParent = $container->getDefinition($def->getParent());
if ($defParent instanceof ChildDefinition) {
$defParent = $container->getDefinition($defParent->getParent());
}

$this->assertSame(RedisTagAwareAdapter::class, $defParent->getClass(), sprintf("'%s' is not %s", $aliasForArgumentStr, RedisTagAwareAdapter::class));
}
}

public function testRemovesResourceCheckerConfigCacheFactoryArgumentOnlyIfNoDebug()
{
$container = $this->createContainer(['kernel.debug' => true]);
Expand Down