-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Cache] Add a Chain adapter #18215
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
[Cache] Add a Chain adapter #18215
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[Cache] Add a Chain adapter
- Loading branch information
commit ebdcd16bdd775eb012c9ec5912edb78f9d9ead04
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?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\CacheItemPoolInterface; | ||
|
|
||
| /** | ||
| * Marker interface for adapters managing {@see \Symfony\Component\Cache\CacheItem} instances. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| interface AdapterInterface extends CacheItemPoolInterface | ||
| { | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| <?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 Psr\Cache\CacheItemPoolInterface; | ||
| use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
|
||
| /** | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PHPdoc of this class could also be reworded a bit. It's confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
| * Chains adapters together. | ||
| * | ||
| * Saves, deletes and clears all registered adapter. | ||
| * Gets data from the first chained adapter having it in cache. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| class ChainAdapter implements AdapterInterface | ||
| { | ||
| private $adapters = array(); | ||
|
|
||
| /** | ||
| * @param AdapterInterface[] $adapters | ||
| */ | ||
| public function __construct(array $adapters) | ||
| { | ||
| if (2 > count($adapters)) { | ||
| throw new InvalidArgumentException('At least two adapters must be chained.'); | ||
| } | ||
|
|
||
| foreach ($adapters as $adapter) { | ||
| if (!$adapter instanceof CacheItemPoolInterface) { | ||
| throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', get_class($adapter), CacheItemPoolInterface::class)); | ||
| } | ||
|
|
||
| if ($adapter instanceof AdapterInterface) { | ||
| $this->adapters[] = $adapter; | ||
| } else { | ||
| $this->adapters[] = new ProxyAdapter($adapter); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getItem($key) | ||
| { | ||
| foreach ($this->adapters as $adapter) { | ||
| $item = $adapter->getItem($key); | ||
|
|
||
| if ($item->isHit()) { | ||
| return $item; | ||
| } | ||
| } | ||
|
|
||
| return $item; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function getItems(array $keys = array()) | ||
| { | ||
| $items = array(); | ||
| foreach ($keys as $key) { | ||
| $items[$key] = $this->getItem($key); | ||
| } | ||
|
|
||
| return $items; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function hasItem($key) | ||
| { | ||
| foreach ($this->adapters as $adapter) { | ||
| if ($adapter->hasItem($key)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function clear() | ||
| { | ||
| $cleared = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $cleared = $adapter->clear() && $cleared; | ||
| } | ||
|
|
||
| return $cleared; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function deleteItem($key) | ||
| { | ||
| $deleted = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $deleted = $adapter->deleteItem($key) && $deleted; | ||
| } | ||
|
|
||
| return $deleted; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function deleteItems(array $keys) | ||
| { | ||
| $deleted = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $deleted = $adapter->deleteItems($keys) && $deleted; | ||
| } | ||
|
|
||
| return $deleted; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function save(CacheItemInterface $item) | ||
| { | ||
| $saved = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $saved = $adapter->save($item) && $saved; | ||
| } | ||
|
|
||
| return $saved; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function saveDeferred(CacheItemInterface $item) | ||
| { | ||
| $saved = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $saved = $adapter->saveDeferred($item) && $saved; | ||
| } | ||
|
|
||
| return $saved; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function commit() | ||
| { | ||
| $committed = true; | ||
|
|
||
| foreach ($this->adapters as $adapter) { | ||
| $committed = $adapter->commit() && $committed; | ||
| } | ||
|
|
||
| return $committed; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?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 Cache\IntegrationTests\CachePoolTest; | ||
| use Symfony\Component\Cache\Adapter\ApcuAdapter; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| use Symfony\Component\Cache\Adapter\ChainAdapter; | ||
| use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter; | ||
|
|
||
| /** | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| class ChainAdapterTest extends CachePoolTest | ||
| { | ||
| protected $skippedTests = array( | ||
| 'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.', | ||
| 'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.', | ||
| 'testDeferredExpired' => 'Failing for now, needs to be fixed.', | ||
| ); | ||
|
|
||
| public function createCachePool() | ||
| { | ||
| if (defined('HHVM_VERSION')) { | ||
| $this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM'; | ||
| } | ||
| if (!function_exists('apcu_fetch') || !ini_get('apc.enabled') || ('cli' === PHP_SAPI && !ini_get('apc.enable_cli'))) { | ||
| $this->markTestSkipped('APCu extension is required.'); | ||
| } | ||
|
|
||
| return new ChainAdapter(array(new ArrayAdapter(), new ExternalAdapter(), new ApcuAdapter(__CLASS__))); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException | ||
| */ | ||
| public function testLessThanTwoAdapterException() | ||
| { | ||
| new ChainAdapter(array()); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException | ||
| */ | ||
| public function testInvalidAdapterException() | ||
| { | ||
| new ChainAdapter(array(new \stdClass(), new \stdClass())); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Cache/Tests/Fixtures/ExternalAdapter.php
992E
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?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\Fixtures; | ||
|
|
||
| use Psr\Cache\CacheItemInterface; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
| use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
|
|
||
| /** | ||
| * Adapter not implementing the {@see \Symfony\Component\Cache\Adapter\AdapterInterface}. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| class ExternalAdapter implements CacheItemPoolInterface | ||
| { | ||
| private $cache; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->cache = new ArrayAdapter(); | ||
| } | ||
|
|
||
| public function getItem($key) | ||
| { | ||
| return $this->cache->getItem($key); | ||
| } | ||
|
|
||
| public function getItems(array $keys = array()) | ||
| { | ||
| return $this->cache->getItems($keys); | ||
| } | ||
|
|
||
| public function hasItem($key) | ||
| { | ||
| return $this->cache->hasItem($key); | ||
| } | ||
|
|
||
| public function clear() | ||
| { | ||
| return $this->cache->clear(); | ||
| } | ||
|
|
||
| public function deleteItem($key) | ||
| { | ||
| return $this->cache->deleteItem($key); | ||
| } | ||
|
|
||
| public function deleteItems(array $keys) | ||
| { | ||
| return $this->cache->deleteItems($keys); | ||
| } | ||
| 5B0B |
|
|
| public function save(CacheItemInterface $item) | ||
| { | ||
| return $this->cache->save($item); | ||
| } | ||
|
|
||
| public function saveDeferred(CacheItemInterface $item) | ||
| { | ||
| return $this->cache->saveDeferred($item); | ||
| } | ||
|
|
||
| public function commit() | ||
| { | ||
| return $this->cache->commit(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't understand this description: