Skip to content

Commit

Permalink
feature #24075 Implemented PruneableInterface on TagAwareAdapter (Tof…
Browse files Browse the repository at this point in the history
…lar)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #24075).

Discussion
----------

Implemented PruneableInterface on TagAwareAdapter

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | See comment on #23451
| License       | MIT
| Doc PR        | Added a comment on symfony/symfony-docs#8209

The `ChainAdapter` already delegates the `prune()` calls. The `TagAwareAdapter` is a wrapping adapter too and should delegate the `prune()` calls just the same.

Commits
-------

b635b62 Implemented PruneableInterface on TagAwareAdapter
  • Loading branch information
nicolas-grekas committed Sep 4, 2017
2 parents 61966df + b635b62 commit 86c3a50
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class TagAwareAdapter implements TagAwareAdapterInterface
class TagAwareAdapter implements TagAwareAdapterInterface, PruneableInterface
{
const TAGS_PREFIX = "\0tags\0";

Expand Down Expand Up @@ -334,4 +335,16 @@ private function getTagVersions(array $tagsByKey)

return $tagVersions;
}

/**
* {@inheritdoc}
*/
public function prune()
{
if ($this->itemsAdapter instanceof PruneableInterface) {
return $this->itemsAdapter->prune();
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
-----

* added PruneableInterface so PSR-6 or PSR-16 cache implementations can declare support for manual stale cache pruning
* added prune logic to FilesystemTrait, PhpFilesTrait, PdoTrait, and ChainTrait
* added prune logic to FilesystemTrait, PhpFilesTrait, PdoTrait, TagAwareAdapter and ChainTrait
* now FilesystemAdapter, PhpFilesAdapter, FilesystemCache, PhpFilesCache, PdoAdapter, PdoCache, ChainAdapter, and
ChainCache implement PruneableInterface and support manual stale cache pruning

Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/TagAwareAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Cache\Tests\Adapter;

use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;

Expand Down Expand Up @@ -125,4 +126,60 @@ public function testGetPreviousTags()
$i = $pool->getItem('k');
$this->assertSame(array('foo' => 'foo'), $i->getPreviousTags());
}

public function testPrune()
{
$cache = new TagAwareAdapter($this->getPruneableMock());
$this->assertTrue($cache->prune());

$cache = new TagAwareAdapter($this->getNonPruneableMock());
$this->assertFalse($cache->prune());

$cache = new TagAwareAdapter($this->getFailingPruneableMock());
$this->assertFalse($cache->prune());
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
*/
private function getPruneableMock()
{
$pruneable = $this
->getMockBuilder(PruneableCacheInterface::class)
->getMock();

$pruneable
->expects($this->atLeastOnce())
->method('prune')
->will($this->returnValue(true));

return $pruneable;
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
*/
private function getFailingPruneableMock()
{
$pruneable = $this
->getMockBuilder(PruneableCacheInterface::class)
->getMock();

$pruneable
->expects($this->atLeastOnce())
->method('prune')
->will($this->returnValue(false));

return $pruneable;
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|AdapterInterface
*/
private function getNonPruneableMock()
{
return $this
->getMockBuilder(AdapterInterface::class)
->getMock();
}
}

0 comments on commit 86c3a50

Please sign in to comment.