2016-01-18 14:59:30 +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;
|
|
|
|
use Symfony\Component\Cache\CacheItem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
|
*/
|
|
|
|
class ProxyAdapter implements CacheItemPoolInterface
|
|
|
|
{
|
|
|
|
private $pool;
|
|
|
|
private $createCacheItem;
|
|
|
|
|
|
|
|
public function __construct(CacheItemPoolInterface $pool)
|
|
|
|
{
|
|
|
|
$this->pool = $pool;
|
|
|
|
$this->createCacheItem = \Closure::bind(
|
|
|
|
function ($key, $value, $isHit) {
|
|
|
|
$item = new CacheItem();
|
|
|
|
$item->key = $key;
|
|
|
|
$item->value = $value;
|
|
|
|
$item->isHit = $isHit;
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
},
|
|
|
|
$this,
|
|
|
|
CacheItem::class
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getItem($key)
|
|
|
|
{
|
|
|
|
$f = $this->createCacheItem;
|
|
|
|
$item = $this->pool->getItem($key);
|
|
|
|
|
|
|
|
return $f($key, $item->get(), $item->isHit());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getItems(array $keys = array())
|
|
|
|
{
|
2016-01-19 11:08:06 +00:00
|
|
|
return $this->generateItems($this->pool->getItems($keys));
|
2016-01-18 14:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function hasItem($key)
|
|
|
|
{
|
|
|
|
return $this->pool->hasItem($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
return $this->pool->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function deleteItem($key)
|
|
|
|
{
|
|
|
|
return $this->pool->deleteItem($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function deleteItems(array $keys)
|
|
|
|
{
|
|
|
|
return $this->pool->deleteItems($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save(CacheItemInterface $item)
|
|
|
|
{
|
|
|
|
return $this->doSave($item, __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function saveDeferred(CacheItemInterface $item)
|
|
|
|
{
|
|
|
|
return $this->doSave($item, __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function commit()
|
|
|
|
{
|
|
|
|
return $this->pool->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doSave(CacheItemInterface $item, $method)
|
|
|
|
{
|
|
|
|
if (!$item instanceof CacheItem) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$item = (array) $item;
|
2016-02-03 09:28:28 +00:00
|
|
|
$expiry = $item[CacheItem::CAST_PREFIX.'expiry'];
|
2016-01-19 11:08:06 +00:00
|
|
|
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
|
|
|
|
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
|
2016-02-03 09:28:28 +00:00
|
|
|
$poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
|
2016-01-18 14:59:30 +00:00
|
|
|
|
|
|
|
return $this->pool->$method($poolItem);
|
|
|
|
}
|
2016-01-19 11:08:06 +00:00
|
|
|
|
|
|
|
private function generateItems($items)
|
|
|
|
{
|
|
|
|
$f = $this->createCacheItem;
|
|
|
|
|
|
|
|
foreach ($items as $key => $item) {
|
|
|
|
yield $key => $f($key, $item->get(), $item->isHit());
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 14:59:30 +00:00
|
|
|
}
|