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/ProxyAdapter.php

140 lines
2.9 KiB
PHP
Raw Normal View History

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())
{
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;
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
$poolItem->expiresAfter($item[CacheItem::CAST_PREFIX.'lifetime']);
2016-01-18 14:59:30 +00:00
return $this->pool->$method($poolItem);
}
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
}