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

199 lines
4.5 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>
*/
2016-01-27 08:06:20 +00:00
class ProxyAdapter implements AdapterInterface
2016-01-18 14:59:30 +00:00
{
private $pool;
private $namespace;
private $namespaceLen;
2016-01-18 14:59:30 +00:00
private $createCacheItem;
private $hits = 0;
private $misses = 0;
2016-01-18 14:59:30 +00:00
public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defaultLifetime = 0)
2016-01-18 14:59:30 +00:00
{
$this->pool = $pool;
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
$this->namespaceLen = strlen($namespace);
2016-01-18 14:59:30 +00:00
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) use ($defaultLifetime) {
2016-01-18 14:59:30 +00:00
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->isHit = $isHit;
$item->defaultLifetime = $defaultLifetime;
2016-01-18 14:59:30 +00:00
return $item;
},
$this,
CacheItem::class
);
}
/**
* {@inheritdoc}
*/
public function getItem($key)
{
$f = $this->createCacheItem;
$item = $this->pool->getItem($this->getId($key));
if ($isHit = $item->isHit()) {
++$this->hits;
} else {
++$this->misses;
}
2016-01-18 14:59:30 +00:00
return $f($key, $item->get(), $isHit);
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function getItems(array $keys = array())
{
if ($this->namespaceLen) {
foreach ($keys as $i => $key) {
$keys[$i] = $this->getId($key);
}
}
return $this->generateItems($this->pool->getItems($keys));
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function hasItem($key)
{
return $this->pool->hasItem($this->getId($key));
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function clear()
{
return $this->pool->clear();
}
/**
* {@inheritdoc}
*/
public function deleteItem($key)
{
return $this->pool->deleteItem($this->getId($key));
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
{
if ($this->namespaceLen) {
foreach ($keys as $i => $key) {
$keys[$i] = $this->getId($key);
}
}
2016-01-18 14:59:30 +00:00
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'];
$poolItem = $this->pool->getItem($this->namespace.$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);
}
private function generateItems($items)
{
$f = $this->createCacheItem;
foreach ($items as $key => $item) {
if ($isHit = $item->isHit()) {
++$this->hits;
} else {
++$this->misses;
}
if ($this->namespaceLen) {
$key = substr($key, $this->namespaceLen);
}
yield $key => $f($key, $item->get(), $isHit);
}
}
/**
* Returns the number of cache read hits.
*
* @return int
*/
public function getHits()
{
return $this->hits;
}
/**
* Returns the number of cache read misses.
*
* @return int
*/
public function getMisses()
{
return $this->misses;
}
private function getId($key)
{
return $this->namespace.CacheItem::validateKey($key);
}
2016-01-18 14:59:30 +00:00
}