@@ -25,12 +25,18 @@ class ArrayAdapter implements CacheItemPoolInterface, LoggerAwareInterface
25
25
{
26
26
use LoggerAwareTrait;
27
27
28
+ private $ storeSerialized ;
28
29
private $ values = array ();
29
30
private $ expiries = array ();
30
31
private $ createCacheItem ;
31
32
32
- public function __construct ($ defaultLifetime = 0 )
33
+ /**
34
+ * @param int $defaultLifetime
35
+ * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise.
36
+ */
37
+ public function __construct ($ defaultLifetime = 0 , $ storeSerialized = true )
33
38
{
39
+ $ this ->storeSerialized = $ storeSerialized ;
34
40
$ this ->createCacheItem = \Closure::bind (
35
41
function ($ key , $ value , $ isHit ) use ($ defaultLifetime ) {
36
42
$ item = new CacheItem ();
@@ -51,10 +57,16 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
51
57
*/
52
58
public function getItem ($ key )
53
59
{
60
+ if (!$ isHit = $ this ->hasItem ($ key )) {
61
+ $ value = null ;
62
+ } elseif ($ this ->storeSerialized ) {
63
+ $ value = unserialize ($ this ->values [$ key ]);
64
+ } else {
65
+ $ value = $ this ->values [$ key ];
66
+ }
54
67
$ f = $ this ->createCacheItem ;
55
- $ isHit = $ this ->hasItem ($ key );
56
68
57
- return $ f ($ key , $ isHit ? $ this -> values [ $ key ] : null , $ isHit );
69
+ return $ f ($ key , $ value , $ isHit );
58
70
}
59
71
60
72
/**
@@ -125,13 +137,12 @@ public function save(CacheItemInterface $item)
125
137
if (0 > $ lifetime ) {
126
138
return true ;
127
139
}
128
-
129
- if (is_object ($ value )) {
140
+ if ($ this ->storeSerialized ) {
130
141
try {
131
- $ value = clone $ value ;
142
+ $ value = serialize ( $ value) ;
132
143
} catch (\Exception $ e ) {
133
144
$ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
134
- CacheItem::log ($ this ->logger , 'Failed to clone key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
145
+ CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
135
146
136
147
return false ;
137
148
}
@@ -179,9 +190,15 @@ private function generateItems(array $keys)
179
190
$ f = $ this ->createCacheItem ;
180
191
181
192
foreach ($ keys as $ key ) {
182
- $ isHit = isset ($ this ->expiries [$ key ]) && ($ this ->expiries [$ key ] >= time () || !$ this ->deleteItem ($ key ));
193
+ if (!$ isHit = isset ($ this ->expiries [$ key ]) && ($ this ->expiries [$ key ] >= time () || !$ this ->deleteItem ($ key ))) {
194
+ $ value = null ;
195
+ } elseif ($ this ->storeSerialized ) {
196
+ $ value = unserialize ($ this ->values [$ key ]);
197
+ } else {
198
+ $ value = $ this ->values [$ key ];
199
+ }
183
200
184
- yield $ key => $ f ($ key , $ isHit ? $ this -> values [ $ key ] : null , $ isHit );
201
+ yield $ key => $ f ($ key , $ value , $ isHit );
185
202
}
186
203
}
187
204
}
0 commit comments