14
14
use Psr \Cache \CacheItemInterface ;
15
15
use Psr \Log \LoggerAwareInterface ;
16
16
use Symfony \Component \Cache \CacheItem ;
17
+ use Symfony \Component \Cache \Serializer \NullSerializer ;
18
+ use Symfony \Component \Cache \Serializer \PhpSerializer ;
17
19
use Symfony \Component \Cache \ResettableInterface ;
18
20
use Symfony \Component \Cache \Traits \ArrayTrait ;
19
21
@@ -32,7 +34,7 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, Resettable
32
34
*/
33
35
public function __construct (int $ defaultLifetime = 0 , bool $ storeSerialized = true )
34
36
{
35
- $ this ->storeSerialized = $ storeSerialized ;
37
+ $ this ->setSerializer ( $ storeSerialized ? new PhpSerializer () : new NullSerializer ()) ;
36
38
$ this ->createCacheItem = \Closure::bind (
37
39
function ($ key , $ value , $ isHit ) use ($ defaultLifetime ) {
38
40
$ item = new CacheItem ();
@@ -57,13 +59,8 @@ public function getItem($key)
57
59
try {
58
60
if (!$ isHit ) {
59
61
$ this ->values [$ key ] = $ value = null ;
60
- } elseif (!$ this ->storeSerialized ) {
61
- $ value = $ this ->values [$ key ];
62
- } elseif ('b:0; ' === $ value = $ this ->values [$ key ]) {
63
- $ value = false ;
64
- } elseif (false === $ value = unserialize ($ value )) {
65
- $ this ->values [$ key ] = $ value = null ;
66
- $ isHit = false ;
62
+ } else {
63
+ $ value = $ this ->unserialize ($ this ->values [$ key ]);
67
64
}
68
65
} catch (\Exception $ e ) {
69
66
CacheItem::log ($ this ->logger , 'Failed to unserialize key "{key}" ' , array ('key ' => $ key , 'exception ' => $ e ));
@@ -117,15 +114,13 @@ public function save(CacheItemInterface $item)
117
114
118
115
return true ;
119
116
}
120
- if ($ this ->storeSerialized ) {
121
- try {
122
- $ value = serialize ($ value );
123
- } catch (\Exception $ e ) {
124
- $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
125
- CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
126
-
127
- return false ;
128
- }
117
+ try {
118
+ $ value = $ this ->serialize ($ value );
119
+ } catch (\Exception $ e ) {
120
+ $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
121
+ CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
122
+
123
+ return false ;
129
124
}
130
125
if (null === $ expiry && 0 < $ item ["\0* \0defaultLifetime " ]) {
131
126
$ expiry = time () + $ item ["\0* \0defaultLifetime " ];
0 commit comments