From 6b0beca36fdbf1b4e1a280ae65ce8729ea87928b Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Thu, 8 Apr 2021 14:13:43 +0200 Subject: [PATCH] [Cache] [FrameworkBundle] Fix logging for TagAwareAdapter --- .../DependencyInjection/FrameworkExtension.php | 6 ++++++ .../DependencyInjection/Fixtures/php/cache.php | 5 +++++ .../DependencyInjection/Fixtures/xml/cache.xml | 1 + .../DependencyInjection/Fixtures/yml/cache.yml | 4 ++++ .../FrameworkExtensionTest.php | 15 +++++++++++++++ .../Component/Cache/Adapter/TagAwareAdapter.php | 5 ++++- src/Symfony/Component/Cache/LockRegistry.php | 2 +- .../Cache/Tests/Adapter/TagAwareAdapterTest.php | 15 +++++++++++++++ 8 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 972c8f255f..b6fad07ad9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1948,6 +1948,12 @@ class FrameworkExtension extends Extension ->setPublic($pool['public']) ; + if (method_exists(TagAwareAdapter::class, 'setLogger')) { + $container + ->getDefinition($name) + ->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); + } + $pool['name'] = $name; $pool['public'] = false; $name = '.'.$name.'.inner'; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php index 8d92edf766..5e607dfdbb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php @@ -32,6 +32,11 @@ $container->loadFromExtension('framework', [ 'redis://foo' => 'cache.adapter.redis', ], ], + 'cache.ccc' => [ + 'adapter' => 'cache.adapter.array', + 'default_lifetime' => 410, + 'tags' => true, + ], ], ], ]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml index 2db74964b5..d53e0764f8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml @@ -17,6 +17,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml index ee20bc74b2..c6c6383715 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml @@ -23,3 +23,7 @@ framework: - cache.adapter.array - cache.adapter.filesystem - {name: cache.adapter.redis, provider: 'redis://foo'} + cache.ccc: + adapter: cache.adapter.array + default_lifetime: 410 + tags: true diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 5e11db8102..35977510bb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -26,6 +26,7 @@ use Symfony\Component\Cache\Adapter\DoctrineAdapter; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Adapter\ProxyAdapter; use Symfony\Component\Cache\Adapter\RedisAdapter; +use Symfony\Component\Cache\Adapter\TagAwareAdapter; use Symfony\Component\Cache\DependencyInjection\CachePoolPass; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -1504,6 +1505,17 @@ abstract class FrameworkExtensionTest extends TestCase 12, ]; $this->assertEquals($expected, $chain->getArguments()); + + // Test "tags: true" wrapping logic + $tagAwareDefinition = $container->getDefinition('cache.ccc'); + $this->assertSame(TagAwareAdapter::class, $tagAwareDefinition->getClass()); + $this->assertCachePoolServiceDefinitionIsCreated($container, (string) $tagAwareDefinition->getArgument(0), 'cache.adapter.array', 410); + + if (method_exists(TagAwareAdapter::class, 'setLogger')) { + $this->assertEquals([ + ['setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]], + ], $tagAwareDefinition->getMethodCalls()); + } } public function testRemovesResourceCheckerConfigCacheFactoryArgumentOnlyIfNoDebug() @@ -1813,6 +1825,9 @@ abstract class FrameworkExtensionTest extends TestCase case 'cache.adapter.redis': $this->assertSame(RedisAdapter::class, $parentDefinition->getClass()); break; + case 'cache.adapter.array': + $this->assertSame(ArrayAdapter::class, $parentDefinition->getClass()); + break; default: $this->fail('Unresolved adapter: '.$adapter); } diff --git a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php index 9a37596865..52ad1e5e54 100644 --- a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php @@ -13,6 +13,8 @@ namespace Symfony\Component\Cache\Adapter; use Psr\Cache\CacheItemInterface; use Psr\Cache\InvalidArgumentException; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\PruneableInterface; use Symfony\Component\Cache\ResettableInterface; @@ -23,11 +25,12 @@ use Symfony\Contracts\Cache\TagAwareCacheInterface; /** * @author Nicolas Grekas */ -class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface +class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface, LoggerAwareInterface { public const TAGS_PREFIX = "\0tags\0"; use ContractsTrait; + use LoggerAwareTrait; use ProxyTrait; private $deferred = []; diff --git a/src/Symfony/Component/Cache/LockRegistry.php b/src/Symfony/Component/Cache/LockRegistry.php index 73057f36c3..d480257d3d 100644 --- a/src/Symfony/Component/Cache/LockRegistry.php +++ b/src/Symfony/Component/Cache/LockRegistry.php @@ -81,7 +81,7 @@ final class LockRegistry public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null) { - $key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1; + $key = self::$files ? abs(crc32($item->getKey())) % \count(self::$files) : -1; if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) { return $callback($item, $save); diff --git a/src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php index d2f6b2877f..adef44b2c3 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Cache\Tests\Adapter; use PHPUnit\Framework\MockObject\MockObject; use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Cache\Adapter\FilesystemAdapter; @@ -196,6 +197,20 @@ class TagAwareAdapterTest extends AdapterTestCase $this->assertFalse($item->isHit()); } + public function testLog() + { + $logger = $this->createMock(LoggerInterface::class); + $logger + ->expects($this->atLeastOnce()) + ->method($this->anything()); + + $cache = new TagAwareAdapter(new ArrayAdapter()); + $cache->setLogger($logger); + + // Computing will produce at least one log + $cache->get('foo', static function (): string { return 'ccc'; }); + } + /** * @return MockObject|PruneableCacheInterface */