8000 [Cache] fix optimizing Psr6Cache for AdapterInterface pools · symfony/symfony@20e7361 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 20e7361

Browse files
[Cache] fix optimizing Psr6Cache for AdapterInterface pools
1 parent 236565c commit 20e7361

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\SimpleCache\CacheException as SimpleCacheException;
1717
use Psr\SimpleCache\CacheInterface;
1818
use Symfony\Component\Cache\Adapter\AbstractAdapter;
19+
use Symfony\Component\Cache\Adapter\AdapterInterface;
1920
use Symfony\Component\Cache\CacheItem;
2021
use Symfony\Component\Cache\Exception\InvalidArgumentException;
2122
use Symfony\Component\Cache\PruneableInterface;
@@ -30,27 +31,34 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
3031
use ProxyTrait;
3132

3233
private $createCacheItem;
34+
private $cacheItemPrototype;
3335

3436
public function __construct(CacheItemPoolInterface $pool)
3537
{
3638
$this->pool = $pool;
3739

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-
);
40+
if (!$pool instanceof AdapterInterface) {
41+
return;
5342
}
43+
$cacheItemPrototype = &$this->cacheItemPrototype;
44+
$createCacheItem = \Closure::bind(
45+
function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
46+
$item = clone $cacheItemPrototype;
47+
$item->key = $allowInt && \is_int($key) ? (string) $key : CacheItem::validateKey($key);
48+
$item->value = $value;
49+
$item->isHit = false;
50+
51+
return $item;
52+
},
53+
null,
54+
CacheItem::class
55+
);
56+
$this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
57+
$this->cacheItemPrototype ?? $this->get($allowInt && \is_int($key) ? (string) $key : $key);
58+
$this->createCacheItem = $createCacheItem;
59+
60+
return $createCacheItem($key, $value, $allowInt);
61+
};
5462
}
5563

5664
/**
@@ -60,6 +68,7 @@ public function get($key, $default = null)
6068
{
6169
try {
6270
$item = $this->pool->getItem($key);
71+
$this->cacheItemPrototype ?? ($this->cacheItemPrototype = clone $item)->set(null);
6372
} catch (SimpleCacheException $e) {
6473
throw $e;
6574
} catch (Psr6CacheException $e) {

0 commit comments

Comments
 (0)
0