8000 [Cache] Add a Chain adapter by nicolas-grekas · Pull Request #18215 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Add a Chain adapter #18215

8000
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 2 commits into from
Mar 27, 2016
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
3 changes: 1 addition & 2 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Cache\Adapter;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
Expand All @@ -21,7 +20,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInterface
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Cache/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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;
use Symfony\Component\Cache\CacheItem;

/**
* Interface for adapters managing instances of Symfony's {@see CacheItem}.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface AdapterInterface extends CacheItemPoolInterface
{
}
3 changes: 1 addition & 2 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Cache\Adapter;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
Expand All @@ -21,7 +20,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ArrayAdapter implements CacheItemPoolInterface, LoggerAwareInterface
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

Expand Down
226 changes: 226 additions & 0 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?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\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

* Chains several adapters together.
*
* Cached items are fetched from the first adapter having them in its data store.
* They are saved and deleted in all adapters at once.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ChainAdapter implements AdapterInterface
{
private $adapters = array();
private $saveUp;

/**
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items.
* @param int $maxLifetime The max lifetime of items propagated from lower adapters to upper ones.
Copy link
Member

Choose a reason for hiding this comment

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

dots at the end of @param lines should be removed

Copy link
Member

Choose a reason for hiding this comment

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

*/
public function __construct(array $adapters, $maxLifetime = 0)
{
if (!$adapters) {
throw new InvalidArgumentException('At least one adapter must be specified.');
}

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);
}
}

$this->saveUp = \Closure::bind(
function ($adapter, $item) use ($maxLifetime) {
$origDefaultLifetime = $item->defaultLifetime;

if (0 < $maxLifetime && ($origDefaultLifetime <= 0 || $maxLifetime < $origDefaultLifetime)) {
$item->defaultLifetime = $maxLifetime;
}

$adapter->save($item);
$item->defaultLifetime = $origDefaultLifetime;
},
$this,
CacheItem::class
);
}

/**
* {@inheritdoc}
*/
public function getItem($key)
{
$saveUp = $this->saveUp;

foreach ($this->adapters as $i => $adapter) {
$item = $adapter->getItem($key);

if ($item->isHit()) {
while (0 <= --$i) {
$saveUp($this->adapters[$i], $item);
Copy link
Contributor

Choose a reason for hiding this comment

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

@nicolas-grekas I don't understand why you save the item with a new lifetime for a fetch operation. Can you explain it to me plz ?

Copy link
Member Author

Choose a reason for hiding this comment

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

because there is no way to know the remaining lifetime of the item in its original pool...

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thx

}

return $item;
}
}

return $item;
}

/**
* {@inheritdoc}
*/
public function getItems(array $keys = array())
{
return $this->generateItems($this->adapters[0]->getItems($keys), 0);
}

private function generateItems($items, $adapterIndex)
{
$missing = array();
$nextAdapterIndex = $adapterIndex + 1;
$nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;

foreach ($items as $k => $item) {
if (!$nextAdapter || $item->isHit()) {
yield $k => $item;
} else {
$missing[] = $k;
}
}

if ($missing) {
$saveUp = $this->saveUp;
$adapter = $this->adapters[$adapterIndex];
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);

foreach ($items as $k => $item) {
if ($item->isHit()) {
$saveUp($adapter, $item);
}

yield $k => $item;
}
}
}

/**
* {@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;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ProxyAdapter implements CacheItemPoolInterface
class ProxyAdapter implements AdapterInterface
{
private $pool;
private $namespace;
Expand Down
54 changes: 54 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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
{
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()));
}

/**
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage At least one adapter must be specified.
*/
public function testEmptyAdaptersException()
{
new ChainAdapter(array());
}

/**
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage The class "stdClass" does not implement
*/
public function testInvalidAdapterException()
{
new ChainAdapter(array(new \stdClass()));
}
}
Loading
0