1717
1818namespace Phpfastcache \Drivers \Mongodb ;
1919
20- use DateTime ;
21- use Exception ;
2220use LogicException ;
2321use MongoClient ;
2422use MongoDB \{BSON \Binary , BSON \UTCDateTime , Client , Collection , Database , DeleteResult , Driver \Command , Driver \Exception \Exception as MongoDBException , Driver \Manager };
@@ -152,7 +150,7 @@ protected function driverRead(CacheItemInterface $item)
152150 if ($ document ) {
153151 $ return = [
154152 self ::DRIVER_DATA_WRAPPER_INDEX => $ this ->decode ($ document [self ::DRIVER_DATA_WRAPPER_INDEX ]->getData ()),
155- self ::DRIVER_TAGS_WRAPPER_INDEX => $ this -> decode ( $ document [self ::DRIVER_TAGS_WRAPPER_INDEX ]->getData () ),
153+ self ::DRIVER_TAGS_WRAPPER_INDEX => $ document [self ::DRIVER_TAGS_WRAPPER_INDEX ]->jsonSerialize ( ),
156154 self ::DRIVER_EDATE_WRAPPER_INDEX => $ document [self ::DRIVER_EDATE_WRAPPER_INDEX ]->toDateTime (),
157155 ];
158156
@@ -193,20 +191,14 @@ protected function driverWrite(CacheItemInterface $item): bool
193191 $ set = [
194192 self ::DRIVER_KEY_WRAPPER_INDEX => $ item ->getKey (),
195193 self ::DRIVER_DATA_WRAPPER_INDEX => new Binary ($ this ->encode ($ item ->get ()), Binary::TYPE_GENERIC ),
196- self ::DRIVER_TAGS_WRAPPER_INDEX => new Binary ( $ this -> encode ( $ item ->getTags ()), Binary:: TYPE_GENERIC ),
197- self ::DRIVER_EDATE_WRAPPER_INDEX => ( $ item -> getTtl () > 0 ? new UTCDateTime (( time () + $ item ->getTtl ()) * 1000 ) : new UTCDateTime ( time () * 1000 )),
194+ self ::DRIVER_TAGS_WRAPPER_INDEX => $ item ->getTags (),
195+ self ::DRIVER_EDATE_WRAPPER_INDEX => new UTCDateTime ($ item ->getExpirationDate ( )),
198196 ];
199197
200198 if (!empty ($ this ->getConfig ()->isItemDetailedDate ())) {
201199 $ set += [
202- self ::DRIVER_MDATE_WRAPPER_INDEX => ($ item ->getModificationDate () ? new UTCDateTime (
203- ($ item ->getModificationDate ()
204- ->getTimestamp ()) * 1000
205- ) : new UTCDateTime (time () * 1000 )),
206- self ::DRIVER_CDATE_WRAPPER_INDEX => ($ item ->getCreationDate () ? new UTCDateTime (
207- ($ item ->getCreationDate ()
208- ->getTimestamp ()) * 1000
209- ) : new UTCDateTime (time () * 1000 )),
200+ self ::DRIVER_MDATE_WRAPPER_INDEX => new UTCDateTime ($ item ->getModificationDate ()),
201+ self ::DRIVER_CDATE_WRAPPER_INDEX => new UTCDateTime ($ item ->getCreationDate ()),
210202 ];
211203 }
212204 $ result = (array )$ this ->getCollection ()->updateOne (
@@ -217,7 +209,7 @@ protected function driverWrite(CacheItemInterface $item): bool
217209 ['upsert ' => true , 'multiple ' => false ]
218210 );
219211 } catch (MongoDBException $ e ) {
220- throw new PhpfastcacheDriverException ('Got an exception while trying to write data to MongoDB server ' , 0 , $ e );
212+ throw new PhpfastcacheDriverException ('Got an exception while trying to write data to MongoDB server: ' . $ e -> getMessage () , 0 , $ e );
221213 }
222214
223215 return isset ($ result ['ok ' ]) ? $ result ['ok ' ] == 1 : true ;
@@ -281,6 +273,16 @@ protected function driverConnect(): bool
281273
282274 if (!$ this ->collectionExists ($ collectionName )) {
283275 $ this ->database ->createCollection ($ collectionName );
276+ $ this ->database ->selectCollection ($ collectionName )
277+ ->createIndex (
278+ [self ::DRIVER_KEY_WRAPPER_INDEX => 1 ],
279+ ['unique ' => true , 'name ' => 'unique_key_index ' ]
280+ );
281+ $ this ->database ->selectCollection ($ collectionName )
282+ ->createIndex (
283+ [self ::DRIVER_EDATE_WRAPPER_INDEX => 1 ],
284+ ['expireAfterSeconds ' => 0 , 'name ' => 'auto_expire_index ' ]
285+ );
284286 }
285287
286288 $ this ->collection = $ this ->database ->selectCollection ($ collectionName );
0 commit comments