Skip to content

[Cache][FrameworkBundle] Fix logging for TagAwareAdapter #40740

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
Apr 11, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,12 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
->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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
'redis://foo' => 'cache.adapter.redis',
],
],
'cache.ccc' => [
'adapter' => 'cache.adapter.array',
'default_lifetime' => 410,
'tags' => true,
],
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -1504,6 +1505,17 @@ public function testCachePoolServices()
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()
Expand Down Expand Up @@ -1813,6 +1825,9 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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;
Expand All @@ -23,11 +25,12 @@
/**
* @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 = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function setFiles(array $files): array

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it better to use sprintf('%u')


if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) {
return $callback($item, $save);
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -196,6 +197,20 @@ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags
$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
*/
Expand Down