[Cache] [FrameworkBundle] Fix logging for TagAwareAdapter

This commit is contained in:
Thomas Calvet 2021-04-08 14:13:43 +02:00
parent 7ce1dda109
commit 6b0beca36f
8 changed files with 51 additions and 2 deletions

View File

@ -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';

View File

@ -32,6 +32,11 @@ $container->loadFromExtension('framework', [
'redis://foo' => 'cache.adapter.redis',
],
],
'cache.ccc' => [
'adapter' => 'cache.adapter.array',
'default_lifetime' => 410,
'tags' => true,
],
],
],
]);

View File

@ -17,6 +17,7 @@
<framework:adapter name="cache.adapter.filesystem" />
<framework:adapter name="cache.adapter.redis" provider="redis://foo" />
</framework:pool>
<framework:pool name="cache.ccc" adapter="cache.adapter.array" default-lifetime="410" tags="true" />
</framework:cache>
</framework:config>
</container>

View File

@ -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

View File

@ -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);
}

View File

@ -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 <p@tchwork.com>
*/
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 = [];

View File

@ -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);

View File

@ -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
*/