1818namespace Phpfastcache \Drivers \Mongodb ;
1919
2020use DateTime ;
21+ use Exception ;
2122use LogicException ;
2223use MongoClient ;
2324use MongoDB \{BSON \Binary , BSON \UTCDateTime , Client , Collection , Database , DeleteResult , Driver \Command , Driver \Exception \Exception as MongoDBException , Driver \Manager };
@@ -75,7 +76,7 @@ public function driverCheck(): bool
7576 public function getStats (): DriverStatistic
7677 {
7778 $ serverStats = $ this ->instance ->getManager ()->executeCommand (
78- ' phpFastCache ' ,
79+ $ this -> getConfig ()-> getDatabaseName () ,
7980 new Command (
8081 [
8182 'serverStatus ' => 1 ,
@@ -87,10 +88,10 @@ public function getStats(): DriverStatistic
8788 )->toArray ()[0 ];
8889
8990 $ collectionStats = $ this ->instance ->getManager ()->executeCommand (
90- ' phpFastCache ' ,
91+ $ this -> getConfig ()-> getDatabaseName () ,
9192 new Command (
9293 [
93- 'collStats ' => ( isset ( $ this ->getConfig ()[ ' collectionName ' ]) ? $ this -> getConfig ()[ ' collectionName ' ] : ' Cache ' ),
94+ 'collStats ' => $ this ->getConfig ()-> getCollectionName ( ),
9495 'verbose ' => true ,
9596 ]
9697 )
@@ -146,25 +147,19 @@ public function getStats(): DriverStatistic
146147 */
147148 protected function driverRead (CacheItemInterface $ item )
148149 {
149- $ document = $ this ->getCollection ()->findOne (['_id ' => $ item -> getEncodedKey ( )]);
150+ $ document = $ this ->getCollection ()->findOne (['_id ' => $ this -> getMongoDbItemKey ( $ item )]);
150151
151152 if ($ document ) {
152153 $ return = [
153154 self ::DRIVER_DATA_WRAPPER_INDEX => $ this ->decode ($ document [self ::DRIVER_DATA_WRAPPER_INDEX ]->getData ()),
154155 self ::DRIVER_TAGS_WRAPPER_INDEX => $ this ->decode ($ document [self ::DRIVER_TAGS_WRAPPER_INDEX ]->getData ()),
155- self ::DRIVER_EDATE_WRAPPER_INDEX => ( new DateTime ())-> setTimestamp ( $ document [self ::DRIVER_EDATE_WRAPPER_INDEX ]->toDateTime ()-> getTimestamp () ),
156+ self ::DRIVER_EDATE_WRAPPER_INDEX => $ document [self ::DRIVER_EDATE_WRAPPER_INDEX ]->toDateTime (),
156157 ];
157158
158159 if (!empty ($ this ->getConfig ()->isItemDetailedDate ())) {
159160 $ return += [
160- self ::DRIVER_MDATE_WRAPPER_INDEX => (new DateTime ())->setTimestamp (
161- $ document [self ::DRIVER_MDATE_WRAPPER_INDEX ]->toDateTime ()
162- ->getTimestamp ()
163- ),
164- self ::DRIVER_CDATE_WRAPPER_INDEX => (new DateTime ())->setTimestamp (
165- $ document [self ::DRIVER_CDATE_WRAPPER_INDEX ]->toDateTime ()
166- ->getTimestamp ()
167- ),
161+ self ::DRIVER_MDATE_WRAPPER_INDEX => $ document [self ::DRIVER_MDATE_WRAPPER_INDEX ]->toDateTime (),
162+ self ::DRIVER_CDATE_WRAPPER_INDEX => $ document [self ::DRIVER_CDATE_WRAPPER_INDEX ]->toDateTime (),
168163 ];
169164 }
170165
@@ -196,6 +191,7 @@ protected function driverWrite(CacheItemInterface $item): bool
196191 if ($ item instanceof Item) {
197192 try {
198193 $ set = [
194+ self ::DRIVER_KEY_WRAPPER_INDEX => $ item ->getKey (),
199195 self ::DRIVER_DATA_WRAPPER_INDEX => new Binary ($ this ->encode ($ item ->get ()), Binary::TYPE_GENERIC ),
200196 self ::DRIVER_TAGS_WRAPPER_INDEX => new Binary ($ this ->encode ($ item ->getTags ()), Binary::TYPE_GENERIC ),
201197 self ::DRIVER_EDATE_WRAPPER_INDEX => ($ item ->getTtl () > 0 ? new UTCDateTime ((time () + $ item ->getTtl ()) * 1000 ) : new UTCDateTime (time () * 1000 )),
@@ -214,7 +210,7 @@ protected function driverWrite(CacheItemInterface $item): bool
214210 ];
215211 }
216212 $ result = (array )$ this ->getCollection ()->updateOne (
217- ['_id ' => $ item -> getEncodedKey ( )],
213+ ['_id ' => $ this -> getMongoDbItemKey ( $ item )],
218214 [
219215 '$set ' => $ set ,
220216 ],
@@ -244,7 +240,7 @@ protected function driverDelete(CacheItemInterface $item): bool
244240 /**
245241 * @var DeleteResult $deletionResult
246242 */
247- $ deletionResult = $ this ->getCollection ()->deleteOne (['_id ' => $ item -> getEncodedKey ( )]);
243+ $ deletionResult = $ this ->getCollection ()->deleteOne (['_id ' => $ this -> getMongoDbItemKey ( $ item )]);
248244
249245 return $ deletionResult ->isAcknowledged ();
250246 }
@@ -257,7 +253,11 @@ protected function driverDelete(CacheItemInterface $item): bool
257253 */
258254 protected function driverClear (): bool
259255 {
260- return $ this ->collection ->deleteMany ([])->isAcknowledged ();
256+ try {
257+ return $ this ->collection ->deleteMany ([])->isAcknowledged ();
258+ } catch (MongoDBException $ e ) {
259+ throw new PhpfastcacheDriverException ('Got error while trying to empty the collection: ' . $ e ->getMessage (), 0 , $ e );
260+ }
261261 }
262262
263263 /**
@@ -294,8 +294,9 @@ protected function driverConnect(): bool
294294 * @param string $databaseName
295295 * @return string The connection URI.
296296 */
297- protected function buildConnectionURI ($ databaseName = '' ): string
297+ protected function buildConnectionURI (string $ databaseName ): string
298298 {
299+ $ databaseName = \urlencode ($ databaseName );
299300 $ servers = $ this ->getConfig ()->getServers ();
300301 $ options = $ this ->getConfig ()->getOptions ();
301302
@@ -332,6 +333,11 @@ static function ($carry, $data) {
332333 );
333334 }
334335
336+ protected function getMongoDbItemKey (CacheItemInterface $ item )
337+ {
338+ return 'pfc_ ' . $ item ->getEncodedKey ();
339+ }
340+
335341 /********************
336342 *
337343 * PSR-6 Extended Methods
0 commit comments