Skip to content

Commit

Permalink
[Cache] fix optimizing Psr6Cache for AdapterInterface pools
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 16, 2018
1 parent 236565c commit b8100a9
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/Symfony/Component/Cache/Simple/Psr6Cache.php
Expand Up @@ -15,7 +15,7 @@
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheException as SimpleCacheException;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
Expand All @@ -30,27 +30,36 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
use ProxyTrait;

private $createCacheItem;
private $cacheItemPrototype;

public function __construct(CacheItemPoolInterface $pool)
{
$this->pool = $pool;

if ($pool instanceof AbstractAdapter) {
$this->createCacheItem = \Closure::bind(
function ($key, $value, $allowInt = false) {
if ($allowInt && \is_int($key)) {
$key = (string) $key;
} else {
CacheItem::validateKey($key);
}
$f = $this->createCacheItem;

return $f($key, $value, false);
},
$pool,
AbstractAdapter::class
);
if (!$pool instanceof AdapterInterface) {
return;
}
$cacheItemPrototype = &$this->cacheItemPrototype;
$createCacheItem = \Closure::bind(
function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
$item = clone $cacheItemPrototype;
$item->key = $allowInt && \is_int($key) ? (string) $key : CacheItem::validateKey($key);
$item->value = $value;
$item->isHit = false;

return $item;
},
null,
CacheItem::class
);
$this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
if (null === $this->cacheItemPrototype) {
$this->get($allowInt && \is_int($key) ? (string) $key : $key);
}
$this->createCacheItem = $createCacheItem;

return $createCacheItem($key, $value, $allowInt);
};
}

/**
Expand All @@ -65,6 +74,10 @@ public function get($key, $default = null)
} catch (Psr6CacheException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
if (null === $this->cacheItemPrototype) {
$this->cacheItemPrototype = clone $item;
$this->cacheItemPrototype->set(null);
}

return $item->isHit() ? $item->get() : $default;
}
Expand Down

0 comments on commit b8100a9

Please sign in to comment.