Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e904ec

Browse files
bug #40740 [Cache][FrameworkBundle] Fix logging for TagAwareAdapter (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache][FrameworkBundle] Fix logging for TagAwareAdapter | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #40108 | License | MIT | Doc PR | - Commits ------- 6b0beca [Cache] [FrameworkBundle] Fix logging for TagAwareAdapter
2 parents 7ce1dda + 6b0beca commit 4e904ec

File tree

8 files changed

+51
-2
lines changed

8 files changed

+51
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,12 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
19481948
->setPublic($pool['public'])
19491949
;
19501950

1951+
if (method_exists(TagAwareAdapter::class, 'setLogger')) {
1952+
$container
1953+
->getDefinition($name)
1954+
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]);
1955+
}
1956+
19511957
$pool['name'] = $name;
19521958
$pool['public'] = false;
19531959
$name = '.'.$name.'.inner';

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
'redis://foo' => 'cache.adapter.redis',
3333
],
3434
],
35+
'cache.ccc' => [
36+
'adapter' => 'cache.adapter.array',
37+
'default_lifetime' => 410,
38+
'tags' => true,
39+
],
3540
],
3641
],
3742
]);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<framework:adapter name="cache.adapter.filesystem" />
1818
<framework:adapter name="cache.adapter.redis" provider="redis://foo" />
1919
</framework:pool>
20+
<framework:pool name="cache.ccc" adapter="cache.adapter.array" default-lifetime="410" tags="true" />
2021
</framework:cache>
2122
</framework:config>
2223
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ framework:
2323
- cache.adapter.array
2424
- cache.adapter.filesystem
2525
- {name: cache.adapter.redis, provider: 'redis://foo'}
26+
cache.ccc:
27+
adapter: cache.adapter.array
28+
default_lifetime: 410
29+
tags: true

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2727
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2828
use Symfony\Component\Cache\Adapter\RedisAdapter;
29+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
2930
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
3031
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
3132
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -1504,6 +1505,17 @@ public function testCachePoolServices()
15041505
12,
15051506
];
15061507
$this->assertEquals($expected, $chain->getArguments());
1508+
1509+
// Test "tags: true" wrapping logic
1510+
$tagAwareDefinition = $container->getDefinition('cache.ccc');
1511+
$this->assertSame(TagAwareAdapter::class, $tagAwareDefinition->getClass());
1512+
$this->assertCachePoolServiceDefinitionIsCreated($container, (string) $tagAwareDefinition->getArgument(0), 'cache.adapter.array', 410);
1513+
1514+
if (method_exists(TagAwareAdapter::class, 'setLogger')) {
1515+
$this->assertEquals([
1516+
['setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]],
1517+
], $tagAwareDefinition->getMethodCalls());
1518+
}
15071519
}
15081520

15091521
public function testRemovesResourceCheckerConfigCacheFactoryArgumentOnlyIfNoDebug()
@@ -1813,6 +1825,9 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con
18131825
case 'cache.adapter.redis':
18141826
$this->assertSame(RedisAdapter::class, $parentDefinition->getClass());
18151827
break;
1828+
case 'cache.adapter.array':
1829+
$this->assertSame(ArrayAdapter::class, $parentDefinition->getClass());
1830+
break;
18161831
default:
18171832
$this->fail('Unresolved adapter: '.$adapter);
18181833
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\InvalidArgumentException;
16+
use Psr\Log\LoggerAwareInterface;
17+
use Psr\Log\LoggerAwareTrait;
1618
use Symfony\Component\Cache\CacheItem;
1719
use Symfony\Component\Cache\PruneableInterface;
1820
use Symfony\Component\Cache\ResettableInterface;
@@ -23,11 +25,12 @@
2325
/**
2426
* @author Nicolas Grekas <[email protected]>
2527
*/
26-
class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
28+
class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface, LoggerAwareInterface
2729
{
2830
public const TAGS_PREFIX = "\0tags\0";
2931

3032
use ContractsTrait;
33+
use LoggerAwareTrait;
3134
use ProxyTrait;
3235

3336
private $deferred = [];

src/Symfony/Component/Cache/LockRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function setFiles(array $files): array
8181

8282
public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null)
8383
{
84-
$key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1;
84+
$key = self::$files ? abs(crc32($item->getKey())) % \count(self::$files) : -1;
8585

8686
if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) {
8787
return $callback($item, $save);

src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use Psr\Cache\CacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
17+
use Psr\Log\LoggerInterface;
1718
use Symfony\Component\Cache\Adapter\AdapterInterface;
1819
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1920
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
@@ -196,6 +197,20 @@ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags
196197
$this->assertFalse($item->isHit());
197198
}
198199

200+
public function testLog()
201+
{
202+
$logger = $this->createMock(LoggerInterface::class);
203+
$logger
204+
->expects($this->atLeastOnce())
205+
->method($this->anything());
206+
207+
$cache = new TagAwareAdapter(new ArrayAdapter());
208+
$cache->setLogger($logger);
209+
210+
// Computing will produce at least one log
211+
$cache->get('foo', static function (): string { return 'ccc'; });
212+
}
213+
199214
/**
200215
* @return MockObject|PruneableCacheInterface
201216
*/

0 commit comments

Comments
 (0)