Permalink
Browse files

bug #29243 [Cache] fix optimizing Psr6Cache for AdapterInterface pool…

…s (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Cache] fix optimizing Psr6Cache for AdapterInterface pools

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -.

As described by @Tobion in https://github.com/symfony/symfony/pull/29236/files#r234324045:
> The problem I have experienced is that in dev mode the cache is decorated with a TraceableCache. This means it loses this optimization and introduces #28918 (comment) again

Commits
-------

b8100a9 [Cache] fix optimizing Psr6Cache for AdapterInterface pools
  • Loading branch information...
nicolas-grekas committed Nov 20, 2018
2 parents 73d9804 + b8100a9 commit ab51fa84a5bc73f3aac9b332bdd9c5fc1b5854fe
Showing with 29 additions and 16 deletions.
  1. +29 −16 src/Symfony/Component/Cache/Simple/Psr6Cache.php
@@ -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;
@@ -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);
};
}
/**
@@ -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;
}

0 comments on commit ab51fa8

Please sign in to comment.