1212namespace Cache \Adapter \MongoDB ;
1313
1414use Cache \Adapter \Common \AbstractCachePool ;
15+ use Cache \Adapter \Common \JsonBinaryArmoring ;
1516use Cache \Adapter \Common \PhpCacheItem ;
1617use Cache \Adapter \Common \TagSupportWithArray ;
1718use MongoDB \Collection ;
2324 */
2425class MongoDBCachePool extends AbstractCachePool
2526{
27+ use JsonBinaryArmoring;
2628 use TagSupportWithArray;
2729
2830 /**
@@ -70,7 +72,12 @@ protected function fetchObjectFromCache($key)
7072 }
7173 }
7274
73- return [true , unserialize ($ object ->data ), unserialize ($ object ->tags ), $ object ->expirationTimestamp ];
75+ return [
76+ true ,
77+ $ this ->thawValue ($ object ->data ),
78+ $ this ->thawValue ($ object ->tags ),
79+ $ object ->expirationTimestamp ,
80+ ];
7481 }
7582
7683 /**
@@ -100,8 +107,8 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl)
100107 {
101108 $ object = [
102109 '_id ' => $ item ->getKey (),
103- 'data ' => serialize ($ item ->get ()),
104- 'tags ' => serialize ($ item ->getTags ()),
110+ 'data ' => $ this -> freezeValue ($ item ->get ()),
111+ 'tags ' => $ this -> freezeValue ($ item ->getTags ()),
105112 'expirationTimestamp ' => $ item ->getExpirationTimestamp (),
106113 ];
107114
@@ -124,7 +131,7 @@ public function getDirectValue($name)
124131 return ;
125132 }
126133
127- return unserialize ($ object ->data );
134+ return $ this -> thawValue ($ object ->data );
128135 }
129136
130137 /**
@@ -134,9 +141,19 @@ public function setDirectValue($name, $value)
134141 {
135142 $ object = [
136143 '_id ' => $ name ,
137- 'data ' => serialize ($ value ),
144+ 'data ' => $ this -> freezeValue ($ value ),
138145 ];
139146
140147 $ this ->collection ->updateOne (['_id ' => $ name ], ['$set ' => $ object ], ['upsert ' => true ]);
141148 }
149+
150+ private function freezeValue ($ value )
151+ {
152+ return static ::jsonArmor (serialize ($ value ));
153+ }
154+
155+ private function thawValue ($ value )
156+ {
157+ return unserialize (static ::jsonDeArmor ($ value ));
158+ }
142159}
0 commit comments