16
16
use Psr \SimpleCache \CacheException as SimpleCacheException ;
17
17
use Psr \SimpleCache \CacheInterface ;
18
18
use Symfony \Component \Cache \Adapter \AbstractAdapter ;
19
+ use Symfony \Component \Cache \Adapter \AdapterInterface ;
19
20
use Symfony \Component \Cache \CacheItem ;
20
21
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
21
22
use Symfony \Component \Cache \PruneableInterface ;
@@ -30,27 +31,34 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
30
31
use ProxyTrait;
31
32
32
33
private $ createCacheItem ;
34
+ private $ cacheItemPrototype ;
33
35
34
36
public function __construct (CacheItemPoolInterface $ pool )
35
37
{
36
38
$ this ->pool = $ pool ;
37
39
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 ;
53
42
}
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
+ };
54
62
}
55
63
56
64
/**
@@ -60,6 +68,7 @@ public function get($key, $default = null)
60
68
{
61
69
try {
62
70
$ item = $ this ->pool ->getItem ($ key );
71
+ $ this ->cacheItemPrototype ?? ($ this ->cacheItemPrototype = clone $ item )->set (null );
63
72
} catch (SimpleCacheException $ e ) {
64
73
throw $ e ;
65
74
} catch (Psr6CacheException $ e ) {
0 commit comments