This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Cache/Adapter/ChainAdapter.php

235 lines
5.7 KiB
PHP
Raw Normal View History

2016-01-27 08:06:20 +00:00
<?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;
2016-03-17 10:12:18 +00:00
use Symfony\Component\Cache\CacheItem;
2016-01-27 08:06:20 +00:00
use Symfony\Component\Cache\Exception\InvalidArgumentException;
/**
2016-03-17 10:12:18 +00:00
* Chains several adapters together.
2016-01-27 08:06:20 +00:00
*
2016-03-17 10:12:18 +00:00
* Cached items are fetched from the first adapter having them in its data store.
* They are saved and deleted in all adapters at once.
2016-01-27 08:06:20 +00:00
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class ChainAdapter implements AdapterInterface
{
private $adapters = array();
private $adapterCount;
2016-03-17 10:12:18 +00:00
private $saveUp;
2016-01-27 08:06:20 +00:00
/**
2016-03-27 13:54:44 +01:00
* @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
2016-01-27 08:06:20 +00:00
*/
2016-03-17 10:12:18 +00:00
public function __construct(array $adapters, $maxLifetime = 0)
2016-01-27 08:06:20 +00:00
{
2016-03-17 10:12:18 +00:00
if (!$adapters) {
throw new InvalidArgumentException('At least one adapter must be specified.');
2016-01-27 08:06:20 +00:00
}
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->adapterCount = count($this->adapters);
2016-03-17 10:12:18 +00:00
$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;
},
null,
2016-03-17 10:12:18 +00:00
CacheItem::class
);
2016-01-27 08:06:20 +00:00
}
/**
* {@inheritdoc}
*/
public function getItem($key)
{
2016-03-17 10:12:18 +00:00
$saveUp = $this->saveUp;
foreach ($this->adapters as $i => $adapter) {
2016-01-27 08:06:20 +00:00
$item = $adapter->getItem($key);
if ($item->isHit()) {
2016-03-17 10:12:18 +00:00
while (0 <= --$i) {
$saveUp($this->adapters[$i], $item);
}
2016-01-27 08:06:20 +00:00
return $item;
}
}
return $item;
}
/**
* {@inheritdoc}
*/
public function getItems(array $keys = array())
{
2016-03-17 10:12:18 +00:00
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;
}
2016-01-27 08:06:20 +00:00
}
2016-03-17 10:12:18 +00:00
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;
}
}
2016-01-27 08:06:20 +00:00
}
/**
* {@inheritdoc}
*/
public function hasItem($key)
{
foreach ($this->adapters as $adapter) {
if ($adapter->hasItem($key)) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function clear()
{
$cleared = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$cleared = $this->adapters[$i]->clear() && $cleared;
2016-01-27 08:06:20 +00:00
}
return $cleared;
}
/**
* {@inheritdoc}
*/
public function deleteItem($key)
{
$deleted = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$deleted = $this->adapters[$i]->deleteItem($key) && $deleted;
2016-01-27 08:06:20 +00:00
}
return $deleted;
}
/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
{
$deleted = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$deleted = $this->adapters[$i]->deleteItems($keys) && $deleted;
2016-01-27 08:06:20 +00:00
}
return $deleted;
}
/**
* {@inheritdoc}
*/
public function save(CacheItemInterface $item)
{
$saved = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$saved = $this->adapters[$i]->save($item) && $saved;
2016-01-27 08:06:20 +00:00
}
return $saved;
}
/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item)
{
$saved = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$saved = $this->adapters[$i]->saveDeferred($item) && $saved;
2016-01-27 08:06:20 +00:00
}
return $saved;
}
/**
* {@inheritdoc}
*/
public function commit()
{
$committed = true;
$i = $this->adapterCount;
2016-01-27 08:06:20 +00:00
while ($i--) {
$committed = $this->adapters[$i]->commit() && $committed;
2016-01-27 08:06:20 +00:00
}
return $committed;
}
}