@@ -31,6 +31,7 @@ class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterfa
31
31
private $ namespace ;
32
32
private $ namespaceLen ;
33
33
private $ createCacheItem ;
34
+ private $ setInnerItem ;
34
35
private $ poolHash ;
35
36
36
37
public function __construct (CacheItemPoolInterface $ pool , string $namespace = '' , int $ defaultLifetime = 0 )
@@ -43,32 +44,52 @@ public function __construct(CacheItemPoolInterface $pool, string $namespace = ''
43
44
function ($ key , $ innerItem ) use ($ defaultLifetime , $ poolHash ) {
44
45
$ item = new CacheItem ();
45
46
$ item ->key = $ key ;
46
- $ item ->value = $ innerItem ->get ();
47
+ $ item ->value = $ v = $ innerItem ->get ();
47
48
$ item ->isHit = $ innerItem ->isHit ();
48
49
$ item ->defaultLifetime = $ defaultLifetime ;
49
50
$ item ->innerItem = $ innerItem ;
50
51
$ item ->poolHash = $ poolHash ;
52
+ if (\is_array ($ v ) && 1 === \count ($ v ) && \is_array ($ v = $ v [CacheItem::STATS_KEY ] ?? null ) && array (0 , 1 ) === \array_keys ($ v )) {
53
+ list ($ item ->value , $ item ->stats ) = $ v ;
54
+ } elseif ($ innerItem instanceof CacheItem) {
55
+ $ item ->stats = $ innerItem ->stats ;
56
+ }
51
57
$ innerItem ->set (null );
52
58
53
59
return $ item ;
54
60
},
55
61
null ,
56
62
CacheItem::class
57
63
);
64
+ $ this ->setInnerItem = \Closure::bind (
65
+ function (CacheItemInterface $ innerItem , array $ item ) {
66
+ if ($ stats = $ item ["\0* \0newStats " ]) {
67
+ $ item ["\0* \0value " ] = array (CacheItem::STATS_KEY => array ($ item ["\0* \0value " ], $ stats ));
68
+ }
69
+ $ innerItem ->set ($ item ["\0* \0value " ]);
70
+ $ innerItem ->expiresAt (null !== $ item ["\0* \0expiry " ] ? \DateTime::createFromFormat ('U ' , $ item ["\0* \0expiry " ]) : null );
71
+ },
72
+ null ,
73
+ CacheItem::class
74
+ );
58
75
}
59
76
60
77
/**
61
78
* {@inheritdoc}
62
79
*/
63
- public function get (string $ key , callable $ callback )
80
+ public function get (string $ key , callable $ callback, float $ beta = null )
64
81
{
65
82
if (!$ this ->pool instanceof CacheInterface) {
66
- return $ this ->doGet ($ this -> pool , $ key , $ callback );
83
+ return $ this ->doGet ($ this , $ key , $ callback, $ beta ?? 1.0 );
67
84
}
68
85
69
86
return $ this ->pool ->get ($ this ->getId ($ key ), function ($ innerItem ) use ($ key , $ callback ) {
70
- return $ callback (($ this ->createCacheItem )($ key , $ innerItem ));
71
- });
87
+ $ item = ($ this ->createCacheItem )($ key , $ innerItem );
88
+ $ item ->set ($ value = $ callback ($ item ));
89
+ ($ this ->setInnerItem )($ innerItem , (array ) $ item );
90
+
91
+ return $ value ;
92
+ }, $ beta );
72
93
}
73
94
74
95
/**
@@ -164,13 +185,11 @@ private function doSave(CacheItemInterface $item, $method)
164
185
return false ;
165
186
}
166
187
$ item = (array ) $ item ;
167
- $ expiry = $ item ["\0* \0expiry " ];
168
- if (null === $ expiry && 0 < $ item ["\0* \0defaultLifetime " ]) {
169
- $ expiry = time () + $ item ["\0* \0defaultLifetime " ];
188
+ if (null === $ item ["\0* \0expiry " ] && 0 < $ item ["\0* \0defaultLifetime " ]) {
189
+ $ item ["\0* \0expiry " ] = time () + $ item ["\0* \0defaultLifetime " ];
170
190
}
171
191
$ innerItem = $ item ["\0* \0poolHash " ] === $ this ->poolHash ? $ item ["\0* \0innerItem " ] : $ this ->pool ->getItem ($ this ->namespace .$ item ["\0* \0key " ]);
172
- $ innerItem ->set ($ item ["\0* \0value " ]);
173
- $ innerItem ->expiresAt (null !== $ expiry ? \DateTime::createFromFormat ('U ' , $ expiry ) : null );
192
+ ($ this ->setInnerItem )($ innerItem , $ item );
174
193
175
194
return $ this ->pool ->$ method ($ innerItem );
176
195
}
0 commit comments