@@ -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,18 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
51
57
*/
52
58
public function getItem ($ key )
53
59
{
60
+ if ($ isHit = $ this ->hasItem ($ key )) {
61
+ if ($ this ->storeSerialized ) {
62
+ $ value = unserialize ($ this ->values [$ key ]);
63
+ } else {
64
+ $ value = $ this ->values [$ key ];
65
+ }
66
+ } else {
67
+ $ value = null ;
68
+ }
54
69
$ f = $ this ->createCacheItem ;
55
- $ isHit = $ this ->hasItem ($ key );
56
70
57
- return $ f ($ key , $ isHit ? $ this -> values [ $ key ] : null , $ isHit );
71
+ return $ f ($ key , $ value , $ isHit );
58
72
}
59
73
60
74
/**
@@ -125,13 +139,12 @@ public function save(CacheItemInterface $item)
125
139
if (0 > $ lifetime ) {
126
140
return true ;
127
141
}
128
-
129
- if (is_object ($ value )) {
142
+ if ($ this ->storeSerialized ) {
130
143
try {
131
- $ value = clone $ value ;
144
+ $ value = serialize ( $ value) ;
132
145
} catch (\Exception $ e ) {
133
146
$ 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 ));
147
+ CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
135
148
136
149
return false ;
137
150
}
@@ -179,9 +192,17 @@ private function generateItems(array $keys)
179
192
$ f = $ this ->createCacheItem ;
180
193
181
194
foreach ($ keys as $ key ) {
182
- $ isHit = isset ($ this ->expiries [$ key ]) && ($ this ->expiries [$ key ] >= time () || !$ this ->deleteItem ($ key ));
195
+ if ($ isHit = isset ($ this ->expiries [$ key ]) && ($ this ->expiries [$ key ] >= time () || !$ this ->deleteItem ($ key ))) {
196
+ if ($ this ->storeSerialized ) {
197
+ $ value = unserialize ($ this ->values [$ key ]);
198
+ } else {
199
+ $ value = $ this ->values [$ key ];
200
+ }
201
+ } else {
202
+ $ value = null ;
203
+ }
183
204
184
- yield $ key => $ f ($ key , $ isHit ? $ this -> values [ $ key ] : null , $ isHit );
205
+ yield $ key => $ f ($ key , $ value , $ isHit );
185
206
}
186
207
}
187
208
}
0 commit comments