1414use Psr \Cache \CacheItemInterface ;
1515use Psr \Log \LoggerAwareInterface ;
1616use Symfony \Component \Cache \CacheItem ;
17+ use Symfony \Component \Cache \Serializer \NullSerializer ;
18+ use Symfony \Component \Cache \Serializer \PhpSerializer ;
1719use Symfony \Component \Cache \ResettableInterface ;
1820use Symfony \Component \Cache \Traits \ArrayTrait ;
1921
@@ -32,7 +34,7 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, Resettable
3234 */
3335 public function __construct (int $ defaultLifetime = 0 , bool $ storeSerialized = true )
3436 {
35- $ this ->storeSerialized = $ storeSerialized ;
37+ $ this ->setSerializer ( $ storeSerialized ? new PhpSerializer () : new NullSerializer ()) ;
3638 $ this ->createCacheItem = \Closure::bind (
3739 function ($ key , $ value , $ isHit ) use ($ defaultLifetime ) {
3840 $ item = new CacheItem ();
@@ -57,13 +59,8 @@ public function getItem($key)
5759 try {
5860 if (!$ isHit ) {
5961 $ 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 ]);
6764 }
6865 } catch (\Exception $ e ) {
6966 CacheItem::log ($ this ->logger , 'Failed to unserialize key "{key}" ' , array ('key ' => $ key , 'exception ' => $ e ));
@@ -117,15 +114,13 @@ public function save(CacheItemInterface $item)
117114
118115 return true ;
119116 }
120- if ($ this ->storeSerialized ) {
121- try {
122- $ value = serialize ($ value );
123- } catch (\Exception<
8000
/span> $ 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 ;
129124 }
130125 if (null === $ expiry && 0 < $ item ["\0* \0defaultLifetime " ]) {
131126 $ expiry = time () + $ item ["\0* \0defaultLifetime " ];
0 commit comments