@@ -46,7 +46,7 @@ public function __construct(CacheItemPoolInterface $pool, string $namespace = ''
46
46
}
47
47
$ this ->namespaceLen = \strlen ($ namespace );
48
48
$ this ->defaultLifetime = $ defaultLifetime ;
49
- self ::$ createCacheItem ?? self :: $ createCacheItem = \Closure::bind (
49
+ self ::$ createCacheItem ??= \Closure::bind (
50
50
static function ($ key , $ innerItem , $ poolHash ) {
51
51
$ item = new CacheItem ();
52
52
$ item ->key = $ key ;
@@ -55,20 +55,12 @@ static function ($key, $innerItem, $poolHash) {
55
55
return $ item ;
56
56
}
57
57
58
- $ item ->value = $ v = $ innerItem ->get ();
58
+ $ item ->value = $ innerItem ->get ();
59
59
$ item ->isHit = $ innerItem ->isHit ();
60
60
$ item ->innerItem = $ innerItem ;
61
61
$ item ->poolHash = $ poolHash ;
62
62
63
- // Detect wrapped values that encode for their expiry and creation duration
64
- // For compactness, these values are packed in the key of an array using
65
- // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
66
- if (\is_array ($ v ) && 1 === \count ($ v ) && 10 === \strlen ($ k = (string ) array_key_first ($ v )) && "\x9D" === $ k [0 ] && "\0" === $ k [5 ] && "\x5F" === $ k [9 ]) {
67
- $ item ->value = $ v [$ k ];
68
- $ v = unpack ('Ve/Nc ' , substr ($ k , 1 , -1 ));
69
- $ item ->metadata [CacheItem::METADATA_EXPIRY ] = $ v ['e ' ] + CacheItem::METADATA_EXPIRY_OFFSET ;
70
- $ item ->metadata [CacheItem::METADATA_CTIME ] = $ v ['c ' ];
71
- } elseif ($ innerItem instanceof CacheItem) {
63
+ if (!$ item ->unpack () && $ innerItem instanceof CacheItem) {
72
64
$ item ->metadata = $ innerItem ->metadata ;
73
65
}
74
66
$ innerItem ->set (null );
@@ -78,17 +70,10 @@ static function ($key, $innerItem, $poolHash) {
78
70
null ,
79
71
CacheItem::class
80
72
);
81
- self ::$ setInnerItem ?? self ::$ setInnerItem = \Closure::bind (
82
- /**
83
- * @param array $item A CacheItem cast to (array); accessing protected properties requires adding the "\0*\0" PHP prefix
84
- */
85
- static function (CacheItemInterface $ innerItem , array $ item ) {
86
- if ($ metadata = $ item ["\0* \0newMetadata " ]) {
87
- // For compactness, expiry and creation duration are packed in the key of an array, using magic numbers as separators
88
- $ item ["\0* \0value " ] = ["\x9D" .pack ('VN ' , (int ) (0.1 + $ metadata [self ::METADATA_EXPIRY ] - self ::METADATA_EXPIRY_OFFSET ), $ metadata [self ::METADATA_CTIME ])."\x5F" => $ item ["\0* \0value " ]];
89
- }
90
- $ innerItem ->set ($ item ["\0* \0value " ]);
91
- $ innerItem ->expiresAt (null !== $ item ["\0* \0expiry " ] ? \DateTime::createFromFormat ('U.u ' , sprintf ('%.6F ' , $ item ["\0* \0expiry " ])) : null );
73
+ self ::$ setInnerItem ??= \Closure::bind (
74
+ static function (CacheItemInterface $ innerItem , CacheItem $ item , $ expiry = null ) {
75
+ $ innerItem ->set ($ item ->pack ());
76
+ $ innerItem ->expiresAt (($ expiry ?? $ item ->expiry ) ? \DateTime::createFromFormat ('U.u ' , sprintf ('%.6F ' , $ expiry ?? $ item ->expiry )) : null );
92
77
},
93
78
null ,
94
79
CacheItem::class
@@ -107,7 +92,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
107
92
return $ this ->pool ->get ($ this ->getId ($ key ), function ($ innerItem , bool &$ save ) use ($ key , $ callback ) {
108
93
$ item = (self ::$ createCacheItem )($ key , $ innerItem , $ this ->poolHash );
109
94
$ item ->set ($ value = $ callback ($ item , $ save ));
110
- (self ::$ setInnerItem )($ innerItem , ( array ) $ item );
95
+ (self ::$ setInnerItem )($ innerItem , $ item );
111
96
112
97
return $ value ;
113
98
}, $ beta , $ metadata );
@@ -208,22 +193,23 @@ private function doSave(CacheItemInterface $item, string $method): bool
208
193
if (!$ item instanceof CacheItem) {
209
194
return false ;
210
195
}
211
- $ item = (array ) $ item ;
212
- if (null === $ item ["\0* \0expiry " ] && 0 < $ this ->defaultLifetime ) {
213
- $ item ["\0* \0expiry " ] = microtime (true ) + $ this ->defaultLifetime ;
196
+ $ castItem = (array ) $ item ;
197
+
198
+ if (null === $ castItem ["\0* \0expiry " ] && 0 < $ this ->defaultLifetime ) {
199
+ $ castItem ["\0* \0expiry " ] = microtime (true ) + $ this ->defaultLifetime ;
214
200
}
215
201
216
- if ($ item ["\0* \0poolHash " ] === $ this ->poolHash && $ item ["\0* \0innerItem " ]) {
217
- $ innerItem = $ item ["\0* \0innerItem " ];
202
+ if ($ castItem ["\0* \0poolHash " ] === $ this ->poolHash && $ castItem ["\0* \0innerItem " ]) {
203
+ $ innerItem = $ castItem ["\0* \0innerItem " ];
218
204
} elseif ($ this ->pool instanceof AdapterInterface) {
219
205
// this is an optimization specific for AdapterInterface implementations
220
206
// so we can save a round-trip to the backend by just creating a new item
221
- $ innerItem = (self ::$ createCacheItem )($ this ->namespace .$ item ["\0* \0key " ], null , $ this ->poolHash );
207
+ $ innerItem = (self ::$ createCacheItem )($ this ->namespace .$ castItem ["\0* \0key " ], null , $ this ->poolHash );
222
208
} else {
223
- $ innerItem = $ this ->pool ->getItem ($ this ->namespace .$ item ["\0* \0key " ]);
209
+ $ innerItem = $ this ->pool ->getItem ($ this ->namespace .$ castItem ["\0* \0key " ]);
224
210
}
225
211
226
- (self ::$ setInnerItem )($ innerItem , $ item );
212
+ (self ::$ setInnerItem )($ innerItem , $ item, $ castItem [ "\0 * \0 expiry " ] );
227
213
228
214
return $ this ->pool ->$ method ($ innerItem );
229
215
}
0 commit comments