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

311 lines
8.0 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;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ContractsTrait;
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>
*/
class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
2016-01-27 08:06:20 +00:00
{
use ContractsTrait;
2016-01-27 08:06:20 +00:00
private $adapters = array();
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
*/
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) {
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);
}
}
$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;
$item->metadata = $sourceItem->metadata;
2016-03-17 10:12:18 +00:00
$sourceItem->isTaggable = false;
unset($sourceItem->metadata[CacheItem::METADATA_TAGS]);
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
},
null,
2016-03-17 10:12:18 +00:00
CacheItem::class
);
2016-01-27 08:06:20 +00:00
}
/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
$lastItem = null;
$i = 0;
2018-11-26 09:38:19 +00:00
$wrap = function (CacheItem $item = null) use ($key, $callback, $beta, &$wrap, &$i, &$lastItem, &$metadata) {
$adapter = $this->adapters[$i];
if (isset($this->adapters[++$i])) {
$callback = $wrap;
$beta = INF === $beta ? INF : 0;
}
if ($adapter instanceof CacheInterface) {
$value = $adapter->get($key, $callback, $beta, $metadata);
} else {
$value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
}
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;
$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;
}
/**
* {@inheritdoc}
*/
public function prune()
{
$pruned = true;
foreach ($this->adapters as $adapter) {
if ($adapter instanceof PruneableInterface) {
$pruned = $adapter->prune() && $pruned;
}
}
return $pruned;
}
/**
* {@inheritdoc}
*/
public function reset()
{
foreach ($this->adapters as $adapter) {
if ($adapter instanceof ResetInterface) {
$adapter->reset();
}
}
}
2016-01-27 08:06:20 +00:00
}