8000 bug #29243 [Cache] fix optimizing Psr6Cache for AdapterInterface pool… · symfony/symfony@ab51fa8 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab51fa8

Browse files
bug #29243 [Cache] fix optimizing Psr6Cache for AdapterInterface pools (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
2 parents 73d9804 + b8100a9 commit ab51fa8

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/Symfony/Component/Cache/Simple/Psr6Cache.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Psr\SimpleCache\CacheException as SimpleCacheException;
1717
use Psr\SimpleCache\CacheInterface;
18-
use Symfony\Component\Cache\Adapter\AbstractAdapter;
18+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1919
use Symfony\Component\Cache\CacheItem;
2020
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2121
use Symfony\Component\Cache\PruneableInterface;
@@ -30,27 +30,36 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
3030
use ProxyTrait;
3131

3232
private $createCacheItem;
33+
private $cacheItemPrototype;
3334

3435
public function __construct(CacheItemPoolInterface $pool)
3536
{
3637
$this->pool = $pool;
3738

38-
if ($pool instanceof AbstractAdapter) {
39-
$this->createCacheItem = \Closure::bind(
40-
function ($key, $value, $allowInt = false) {
41-
if ($allowInt && \is_int($key)) {
42-
$key = (string) $key;
43-
} else {
44-
CacheItem::validateKey($key);
45-
}
46-
$f = $this->createCacheItem;
47-
48-
return $f($key, $value, false);
49-
},
50-
$pool,
51-
AbstractAdapter::class
52-
);
39+
if (!$pool instanceof AdapterInterface) {
40+
return;
5341
}
42+
$cacheItemPrototype = &$this->cacheItemPrototype;
43+
$createCacheItem = \Closure::bind(
44+
function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
45+
$item = clone $cacheItemPrototype;
46+
$item->key = $allowInt && \is_int($key) ? (string) $key : CacheItem::validateKey($key);
47+
$item->value = $value;
48+
$item->isHit = false;
49+
50+
return $item;
51+
},
52+
null,
53+
CacheItem::class
54+
);
55+
$this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
56+
if (null === $this->cacheItemPrototype) {
57+
$this->get($allowInt && \is_int($key) ? (string) $key : $key);
58+
}
59+
$this->createCacheItem = $createCacheItem;
60+
61+
return $createCacheItem($key, $value, $allowInt);
62+
};
5463
}
5564

5665
/**
@@ -65,6 +74,10 @@ public function get($key, $default = null)
6574
} catch (Psr6CacheException $e) {
6675
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
6776
}
77+
if (null === $this->cacheItemPrototype) {
78+
$this->cacheItemPrototype = clone $item;
79+
$this->cacheItemPrototype->set(null);
80+
}
6881

6982
return $item->isHit() ? $item->get() : $default;
7083
}

0 commit comments

Comments
 (0)
0