@@ -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,58 @@ 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 ) && 10 === \strlen ($ k = \key ($ v )) && "\x9D" === $ k [0 ] && "\0" === $ k [5 ] && "\x5F" === $ k [9 ]) {
53
+ $ item ->value = $ v [$ k ];
54
+ $ v = \unpack ('Ve/Nc ' , \substr ($ k , 1 , -1 ));
55
+ $ item ->stats [CacheItem::STATS_EXPIRY ] = $ v ['e ' ] + CacheItem::STATS_EXPIRY_OFFSET ;
56
+ $ item ->stats [CacheItem::STATS_CTIME ] = $ v ['c ' ];
57
+ } elseif ($ innerItem instanceof CacheItem) {
58
+ $ item ->stats = $ innerItem ->stats ;
59
+ }
51
60
$ innerItem ->set (null );
52
61
53
62
return $ item ;
54
63
},
55
64
null ,
56
65
CacheItem::class
57
66
);
67
+ $ this ->setInnerItem = \Closure::bind (
68
+ function (CacheItemInterface $ innerItem , array $ item ) {
69
+ if (isset (($ stats = $ item ["\0* \0newStats " ])[CacheItem::STATS_TAGS ])) {
70
+ unset($ stats [CacheItem::STATS_TAGS ]);
71
+ }
72
+ if ($ stats ) {
73
+ $ item ["\0* \0value " ] = array ("\x9D" .pack ('VN ' , $ stats [CacheItem::STATS_EXPIRY ] - CacheItem::STATS_EXPIRY_OFFSET , $ stats [CacheItem::STATS_CTIME ])."\x5F" => $ item ["\0* \0value " ]);
74
+ }
75
+ $ innerItem ->set ($ item ["\0* \0value " ]);
76
+ $ innerItem ->expiresAt (null !== $ item ["\0* \0expiry " ] ? \DateTime::createFromFormat ('U ' , $ item ["\0* \0expiry " ]) : null );
77
+ },
78
+ null ,
79
+ CacheItem::class
80
+ );
58
81
}
59
82
60
83
/**
61
84
* {@inheritdoc}
62
85
*/
63
- public function get (string $ key , callable $ callback )
86
+ public function get (string $ key , callable $ callback, float $ beta = null )
64
87
{
65
88
if (!$ this ->pool instanceof CacheInterface) {
66
- return $ this ->doGet ($ this -> pool , $ key , $ callback );
89
+ return $ this ->doGet ($ this , $ key , $ callback, $ beta ?? 1.0 );
67
90
}
68
91
69
92
return $ this ->pool ->get ($ this ->getId ($ key ), function ($ innerItem ) use ($ key , $ callback ) {
70
- return $ callback (($ this ->createCacheItem )($ key , $ innerItem ));
71
- });
93
+ $ item = ($ this ->createCacheItem )($ key , $ innerItem );
94
+ $ item ->set ($ value = $ callback ($ item ));
95
+ ($ this ->setInnerItem )($ innerItem , (array ) $ item );
96
+
97
+ return $ value ;
98
+ }, $ beta );
72
99
}
73
100
74
101
/**
@@ -164,13 +191,11 @@ private function doSave(CacheItemInterface $item, $method)
164
191
return false ;
165
192
}
166
193
$ item = (array ) $ item ;
167
- $ expiry = $ item ["\0* \0expiry " ];
168
- if (null === $ expiry && 0 < $ item ["\0* \0defaultLifetime " ]) {
169
- $ expiry = time () + $ item ["\0* \0defaultLifetime " ];
194
+ if (null === $ item ["\0* \0expiry " ] && 0 < $ item ["\0* \0defaultLifetime " ]) {
195
+ $ item ["\0* \0expiry " ] = time () + $ item ["\0* \0defaultLifetime " ];
170
196
}
171
197
$ 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 );
198
+ ($ this ->setInnerItem )($ innerItem , $ item );
174
199
175
200
return $ this ->pool ->$ method ($ innerItem );
176
201
}
0 commit comments