@@ -31,6 +31,7 @@ class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterfa
3131 private $ namespace ;
3232 private $ namespaceLen ;
3333 private $ createCacheItem ;
34+ private $ setInnerItem ;
3435 private $ poolHash ;
3536
3637 public function __construct (CacheItemPoolInterface $ pool , string $ namespace = '' , int $ defaultLifetime = 0 )
@@ -43,32 +44,58 @@ public function __construct(CacheItemPoolInterface $pool, string $namespace = ''
4344 function ($ key , $ innerItem ) use ($ defaultLifetime , $ poolHash ) {
4445 $ item = new CacheItem ();
4546 $ item ->key = $ key ;
46- $ item ->value = $ innerItem ->get ();
47+ $ item ->value = $ v = $ innerItem ->get ();
4748 $ item ->isHit = $ innerItem ->isHit ();
4849 $ item ->defaultLifetime = $ defaultLifetime ;
4950 $ item ->innerItem = $ innerItem ;
5051 $ 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+ }
5160 $ innerItem ->set (null );
5261
5362 return $ item ;
5463 },
5564 null ,
5665 CacheItem::class
5766 );
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+ );
5881 }
5982
6083 /**
6184 * {@inheritdoc}
6285 */
63- public function get (string $ key , callable $ callback )
86+ public function get (string $ key , callable $ callback, float $ beta = null )
6487 {
6588 if (!$ this ->pool instanceof CacheInterface) {
66- return $ this ->doGet ($ this -> pool , $ key , $ callback );
89+ return $ this ->doGet ($ this , $ key , $ callback, $ beta ?? 1.0 );
6790 }
6891
6992 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 );
7299 }
73100
74101 /**
@@ -164,13 +191,11 @@ private function doSave(CacheItemInterface $item, $method)
164191 return false ;
165192 }
166193 $ 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 " ];
170196 }
171197 $ 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 );
174199
175200 return $ this ->pool ->$ method ($ innerItem );
176201 }
0 commit comments