Skip to content

[Cache] Allow decorating each cache.pool as debug adapters #42241

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass;
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass;
use Symfony\Component\Cache\DependencyInjection\DebugCachePoolPass;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
Expand Down Expand Up @@ -166,6 +167,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
$container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
$this->addCompilerPassIfExists($container, DebugCachePoolPass::class);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Symfony\Component\Cache\DataCollector\CacheDataCollector;

return static function (ContainerConfigurator $container) {
$container->parameters()
->set('cache.exception_on_save', false)
;

$container->services()
// DataCollector (public to prevent inlining, made private in CacheCollectorPass)
->set('data_collector.cache', CacheDataCollector::class)
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/Cache/Adapter/DebugAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Adapter;

use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Exception\CacheException;

class DebugAdapter extends ProxyAdapter
{
/**
* @throws CacheException in case cache item could not get saved
*/
public function save(CacheItemInterface $item)
{
$result = parent::save($item);

if (!$result) {
throw new CacheException("Can not save cache item with key '{$item->getKey()}'.");
}

return $result;
}

/**
* @throws CacheException in case cache item could not get saved
*/
public function saveDeferred(CacheItemInterface $item)
{
$result = parent::saveDeferred($item);

if (!$result) {
throw new CacheException("Can not save cache item with key '{$item->getKey()}'.");
}

return $result;
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/Cache/Adapter/DebugTagAwareAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Adapter;

use Symfony\Component\Cache\Traits\ProxyTrait;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

final class DebugTagAwareAdapter extends DebugAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface
{
use ProxyTrait;

public function invalidateTags(array $tags): bool
{
return $this->pool->invalidateTags($tags);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Make `LockRegistry` use semaphores when possible
* Deprecate `DoctrineProvider` because this class has been added to the `doctrine/cache` package
* Introduce `DebugAdapter` that throws exception when save resulted in failure

5.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\DependencyInjection;

use Symfony\Component\Cache\Adapter\DebugAdapter;
use Symfony\Component\Cache\Adapter\DebugTagAwareAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class DebugCachePoolPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->getParameter('cache.exception_on_save')) {
return;
}

foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
$decoratablePool = $container->getDefinition($id);
if ($decoratablePool->isAbstract()) {
continue;
}

$decoratingClass = DebugAdapter::class;
if (is_subclass_of($decoratablePool->getClass(), TagAwareAdapterInterface::class)) {
$decoratingClass = DebugTagAwareAdapter::class;
}
$renamedServiceId = $id.'.inner_adapter';

$decoratingPool = new Definition($decoratingClass, [$decoratablePool]);
$decoratingPool->setArguments([new Reference($renamedServiceId)]);
$decoratingPool->setDecoratedService($id, $renamedServiceId);

$container->set($id, $decoratingPool);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Cache/LockRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ final class LockRegistry
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ArrayAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ChainAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'CouchbaseBucketAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DebugAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DebugTagAwareAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemAdapter.php',
__DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemTagAwareAdapter.php',
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/DebugAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Exception\CacheException;

class DebugAdapterTest extends TestCase
{
private $adapter;

protected function setUp(): void
{
$arrayAdapter = $this->createMock(ArrayAdapter::class);
$arrayAdapter->method('save')->willReturn(false);

$this->adapter = new DebugAdapter($arrayAdapter);
}

public function testExceptionOnSave()
{
/** @var CacheItemInterface $cacheItem */
$cacheItem = $this->adapter->getItem('sample-key');

$this->expectException(CacheException::class);
$this->adapter->save($cacheItem);
}

public function testExceptionOnSaveDeferred()
{
/** @var CacheItemInterface $cacheItem */
$cacheItem = $this->adapter->getItem('sample-key');

$this->expectException(CacheException::class);
$this->adapter->saveDeferred($cacheItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\DebugAdapter;
use Symfony\Component\Cache\Adapter\DebugTagAwareAdapter;
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
use Symfony\Component\Cache\DependencyInjection\DebugCachePoolPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class DebugCachePoolPassTest extends TestCase
{
/** @var DebugCachePoolPass */
private $cachePoolPass;

protected function setUp(): void
{
$this->cachePoolPass = new DebugCachePoolPass();
}

public function testProcess()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.project_dir', 'foo');
$container->setParameter('cache.exception_on_save', 'true');

$container->register('cache.adapter.array', ArrayAdapter::class)
->addTag('cache.pool');

$container->register('cache.adapter.filesystem_tag', FilesystemTagAwareAdapter::class)
->addTag('cache.pool');

$this->cachePoolPass->process($container);

/** @var Definition $decoratedAdapter */
$decoratedAdapter = $container->get('cache.adapter.array');
$this->assertEquals(DebugAdapter::class, $decoratedAdapter->getClass());

/** @var Definition $decoratedAdapter */
$decoratedAdapter = $container->get('cache.adapter.filesystem_tag');
$this->assertEquals(DebugTagAwareAdapter::class, $decoratedAdapter->getClass());
}
}