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;
|
2017-07-20 18:13:39 +01:00
|
|
|
use Symfony\Component\Cache\PruneableInterface;
|
2017-09-15 21:03:33 +01:00
|
|
|
use Symfony\Component\Cache\ResettableInterface;
|
2018-04-15 01:21:41 +01:00
|
|
|
use Symfony\Component\Cache\Traits\GetTrait;
|
2018-08-01 09:02:13 +01:00
|
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
2018-07-13 18:06:58 +01:00
|
|
|
use Symfony\Contracts\Service\ResetInterface;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
*/
|
2018-04-15 01:21:41 +01:00
|
|
|
class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
|
2016-01-27 08:06:20 +00:00
|
|
|
{
|
2018-04-15 01:21:41 +01:00
|
|
|
use GetTrait;
|
|
|
|
|
2016-01-27 08:06:20 +00:00
|
|
|
private $adapters = array();
|
2017-01-04 21:53:34 +00:00
|
|
|
private $adapterCount;
|
2018-04-19 15:46:47 +01:00
|
|
|
private $syncItem;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
|
|
|
/**
|
2018-04-19 15:46:47 +01:00
|
|
|
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
|
|
|
|
* @param int $defaultLifetime The default lifetime of items propagated from lower adapters to upper ones
|
2016-01-27 08:06:20 +00:00
|
|
|
*/
|
2018-04-20 11:04:09 +01:00
|
|
|
public function __construct(array $adapters, int $defaultLifetime = 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) {
|
2018-07-26 09:45:46 +01:00
|
|
|
throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', \get_class($adapter), CacheItemPoolInterface::class));
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($adapter instanceof AdapterInterface) {
|
|
|
|
$this->adapters[] = $adapter;
|
|
|
|
} else {
|
|
|
|
$this->adapters[] = new ProxyAdapter($adapter);
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 09:45:46 +01:00
|
|
|
$this->adapterCount = \count($this->adapters);
|
2016-03-17 10:12:18 +00:00
|
|
|
|
2018-04-19 15:46:47 +01:00
|
|
|
$this->syncItem = \Closure::bind(
|
|
|
|
function ($sourceItem, $item) use ($defaultLifetime) {
|
|
|
|
$item->value = $sourceItem->value;
|
|
|
|
$item->expiry = $sourceItem->expiry;
|
|
|
|
$item->isHit = $sourceItem->isHit;
|
2018-04-23 17:02:04 +01:00
|
|
|
$item->metadata = $sourceItem->metadata;
|
2016-03-17 10:12:18 +00:00
|
|
|
|
2018-04-15 01:21:41 +01:00
|
|
|
$sourceItem->isTaggable = false;
|
2018-04-23 17:02:04 +01:00
|
|
|
unset($sourceItem->metadata[CacheItem::METADATA_TAGS]);
|
2018-04-15 01:21:41 +01:00
|
|
|
|
2018-04-19 15:46:47 +01:00
|
|
|
if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
|
|
|
|
$defaultLifetime = $sourceItem->defaultLifetime;
|
|
|
|
}
|
|
|
|
if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
|
|
|
|
$item->defaultLifetime = $defaultLifetime;
|
2016-03-17 10:12:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 15:46:47 +01:00
|
|
|
return $item;
|
2016-03-17 10:12:18 +00:00
|
|
|
},
|
2016-06-13 15:28:45 +01:00
|
|
|
null,
|
2016-03-17 10:12:18 +00:00
|
|
|
CacheItem::class
|
|
|
|
);
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 01:21:41 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-04-23 17:02:04 +01:00
|
|
|
public function get(string $key, callable $callback, float $beta = null)
|
2018-04-15 01:21:41 +01:00
|
|
|
{
|
|
|
|
$lastItem = null;
|
|
|
|
$i = 0;
|
2018-04-23 17:02:04 +01:00
|
|
|
$wrap = function (CacheItem $item = null) use ($key, $callback, $beta, &$wrap, &$i, &$lastItem) {
|
2018-04-15 01:21:41 +01:00
|
|
|
$adapter = $this->adapters[$i];
|
|
|
|
if (isset($this->adapters[++$i])) {
|
|
|
|
$callback = $wrap;
|
2018-04-23 17:02:04 +01:00
|
|
|
$beta = INF === $beta ? INF : 0;
|
2018-04-15 01:21:41 +01:00
|
|
|
}
|
|
|
|
if ($adapter instanceof CacheInterface) {
|
2018-04-23 17:02:04 +01:00
|
|
|
$value = $adapter->get($key, $callback, $beta);
|
2018-04-15 01:21:41 +01:00
|
|
|
} else {
|
2018-09-24 18:16:30 +01:00
|
|
|
$value = $this->doGet($adapter, $key, $callback, $beta);
|
2018-04-15 01:21:41 +01:00
|
|
|
}
|
|
|
|
if (null !== $item) {
|
|
|
|
($this->syncItem)($lastItem = $lastItem ?? $item, $item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
};
|
|
|
|
|
|
|
|
return $wrap();
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:06:20 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getItem($key)
|
|
|
|
{
|
2018-04-19 15:46:47 +01:00
|
|
|
$syncItem = $this->syncItem;
|
|
|
|
$misses = array();
|
2016-03-17 10:12:18 +00:00
|
|
|
|
|
|
|
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) {
|
2018-04-19 15:46:47 +01:00
|
|
|
$this->adapters[$i]->save($syncItem($item, $misses[$i]));
|
2016-03-17 10:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 08:06:20 +00:00
|
|
|
return $item;
|
|
|
|
}
|
2018-04-19 15:46:47 +01:00
|
|
|
|
|
|
|
$misses[$i] = $item;
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-04-19 15:46:47 +01:00
|
|
|
$misses = array();
|
2016-03-17 10:12:18 +00:00
|
|
|
$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;
|
2018-04-19 15:46:47 +01:00
|
|
|
$misses[$k] = $item;
|
2016-03-17 10:12:18 +00:00
|
|
|
}
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 10:12:18 +00:00
|
|
|
if ($missing) {
|
2018-04-19 15:46:47 +01:00
|
|
|
$syncItem = $this->syncItem;
|
2016-03-17 10:12:18 +00:00
|
|
|
$adapter = $this->adapters[$adapterIndex];
|
|
|
|
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
|
|
|
|
|
|
|
|
foreach ($items as $k => $item) {
|
|
|
|
if ($item->isHit()) {
|
2018-04-19 15:46:47 +01:00
|
|
|
$adapter->save($syncItem($item, $misses[$k]));
|
2016-03-17 10:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +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;
|
2017-01-04 21:53:34 +00:00
|
|
|
$i = $this->adapterCount;
|
2016-01-27 08:06:20 +00:00
|
|
|
|
2017-01-04 21:53:34 +00:00
|
|
|
while ($i--) {
|
|
|
|
$committed = $this->adapters[$i]->commit() && $committed;
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $committed;
|
|
|
|
}
|
2017-07-20 18:13:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function prune()
|
|
|
|
{
|
|
|
|
$pruned = true;
|
|
|
|
|
|
|
|
foreach ($this->adapters as $adapter) {
|
|
|
|
if ($adapter instanceof PruneableInterface) {
|
|
|
|
$pruned = $adapter->prune() && $pruned;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pruned;
|
|
|
|
}
|
2017-09-15 21:03:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
foreach ($this->adapters as $adapter) {
|
2018-04-29 15:58:54 +01:00
|
|
|
if ($adapter instanceof ResetInterface) {
|
2017-09-15 21:03:33 +01:00
|
|
|
$adapter->reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 08:06:20 +00:00
|
|
|
}
|