* * 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 */ 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)); } /** * {@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; $expiry = $item[CacheItem::CAST_PREFIX.'expiry']; $poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']); $poolItem->set($item[CacheItem::CAST_PREFIX.'value']); $poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null); 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()); } } }