@@ -223,6 +223,7 @@ Store Scope Blocking Expiring
223
223
============================================ ====== ======== ========
224
224
:ref: `FlockStore <lock-store-flock >` local yes no
225
225
:ref: `MemcachedStore <lock-store-memcached >` remote no yes
226
+ :ref: `MongoDbStore <lock-store-mongodb >` remote no yes
226
227
:ref: `PdoStore <lock-store-pdo >` remote no yes
227
228
:ref: `RedisStore <lock-store-redis >` remote no yes
228
229
:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
@@ -275,6 +276,39 @@ support blocking, and expects a TTL to avoid stalled locks::
275
276
276
277
Memcached does not support TTL lower than 1 second.
277
278
279
+ .. _lock-store-mongodb :
280
+
281
+ MongoDbStore
282
+ ~~~~~~~~~~~~
283
+
284
+ .. versionadded :: 4.4
285
+
286
+ The ``MongoDbStore `` was introduced in Symfony 4.4.
287
+
288
+ The MongoDbStore saves locks on a MongoDB server, it requires a
289
+ ``\MongoDB\Client `` connection from `mongodb/mongodb `_. This store does not
290
+ support blocking and expects a TTL to avoid stalled locks::
291
+
292
+ use Symfony\Component\Lock\Store\MongoDbStore;
293
+
294
+ $mongoClient = new \MongoDB\Client('mongo://localhost/');
295
+
296
+ $options = [
297
+ 'database' => 'my-app',
298
+ ];
299
+
300
+ $store = new MongoDbStore($mongoClient, $options);
301
+
302
+ The ``MongoDbStore `` takes the following ``$options ``:
303
+
304
+ ============ ========= ========================================================================
305
+ Option Default Description
306
+ ============ ========= ========================================================================
307
+ database The name of the database [Mandatory]
308
+ collection ``lock `` The name of the collection
309
+ gcProbablity ``0.001 `` Should a TTL Index be created expressed as a probability from 0.0 to 1.0
310
+ ============ ========= ========================================================================
311
+
278
312
.. _lock-store-pdo :
279
313
280
314
PdoStore
@@ -411,7 +445,7 @@ Remote Stores
411
445
~~~~~~~~~~~~~
412
446
413
447
Remote stores (:ref: `MemcachedStore <lock-store-memcached >`,
414
- :ref: `PdoStore <lock-store-pdo >`,
448
+ :ref: `MongoDbStore < lock-store-mongodb >`, :ref: ` PdoStore <lock-store-pdo >`,
415
449
:ref: `RedisStore <lock-store-redis >` and
416
450
:ref: `ZookeeperStore <lock-store-zookeeper >`) use a unique token to recognize
417
451
the true owner of the lock. This token is stored in the
@@ -436,7 +470,7 @@ Expiring Stores
436
470
~~~~~~~~~~~~~~~
437
471
438
472
Expiring stores (:ref: `MemcachedStore <lock-store-memcached >`,
439
- :ref: `PdoStore <lock-store-pdo >` and
473
+ :ref: `MongoDbStore < lock-store-mongodb >`, :ref: ` PdoStore <lock-store-pdo >` and
440
474
:ref: `RedisStore <lock-store-redis >`)
441
475
guarantee that the lock is acquired only for the defined duration of time. If
442
476
the task takes longer to be accomplished, then the lock can be released by the
@@ -554,6 +588,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
554
588
The method ``flush() `` must not be called, or locks should be stored in a
555
589
dedicated Memcached service away from Cache.
556
590
591
+ MongoDbStore
592
+ ~~~~~~~~~~~~
593
+
594
+ .. caution ::
595
+
596
+ The locked resource name is indexed in the ``_id `` field of the lock
597
+ collection. Beware that in MongoDB an indexed field's value can be
598
+ `a maximum of 1024 bytes in length `_ inclusive of structural overhead.
599
+
600
+ A TTL index MUST BE used on MongoDB 2.2+ to automatically clean up expired locks.
601
+ Such an index can be created manually:
602
+
603
+ .. code-block :: javascript
604
+
605
+ db .lock .ensureIndex (
606
+ { " expires_at" : 1 },
607
+ { " expireAfterSeconds" : 0 }
608
+ )
609
+
610
+ Alternatively, the method ``MongoDbStore::createTtlIndex(int $expireAfterSeconds = 0) ``
611
+ can be called once to create the TTL index during database setup. Read more
612
+ about `Expire Data from Collections by Setting TTL `_ in MongoDB.
613
+
614
+ .. tip ::
615
+
616
+ ``MongoDbStore `` will attempt to automatically create a TTL index on MongoDB
617
+ 2.2+. It's recommended to set constructor option ``gcProbablity = 0.0 `` to
618
+ disable this behavior if you have manually dealt with TTL index creation.
619
+
620
+ .. caution ::
621
+
622
+ This store relies on all PHP application and database nodes to have
623
+ synchronized clocks for lock expiry to occur at the correct time. To ensure
624
+ locks don't expire prematurely; the lock TTL should be set with enough extra
625
+ time in ``expireAfterSeconds `` to account for any clock drift between nodes.
626
+
627
+ ``writeConcern ``, ``readConcern `` and ``readPreference `` are not specified by
628
+ MongoDbStore meaning the collection's settings will take effect. Read more
629
+ about `Replica Set Read and Write Semantics `_ in MongoDB.
630
+
557
631
PdoStore
558
632
~~~~~~~~~~
559
633
@@ -672,10 +746,14 @@ instance, during the deployment of a new version. Processes with new
672
746
configuration must not be started while old processes with old configuration
673
747
are still running.
674
748
749
+ .. _`a maximum of 1024 bytes in length` : https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
675
750
.. _`ACID` : https://en.wikipedia.org/wiki/ACID
751
+ .. _`Data Source Name (DSN)` : https://en.wikipedia.org/wiki/Data_source_name
752
+ .. _`Doctrine DBAL Connection` : https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
753
+ .. _`Expire Data from Collections by Setting TTL` : https://docs.mongodb.com/manual/tutorial/expire-data/
676
754
.. _`locks` : https://en.wikipedia.org/wiki/Lock_(computer_science)
677
- .. _`PHP semaphore functions ` : https://php.net/manual/en/book.sem.php
755
+ .. _`mongodb/mongodb ` : https://packagist.org/packages/mongodb/mongodb
678
756
.. _`PDO` : https://php.net/pdo
679
- .. _`Doctrine DBAL Connection ` : https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection .php
680
- .. _`Data Source Name (DSN) ` : https://en.wikipedia.org/wiki/Data_source_name
757
+ .. _`PHP semaphore functions ` : https://php.net/manual/en/book.sem .php
758
+ .. _`Replica Set Read and Write Semantics ` : https://docs.mongodb.com/manual/applications/replication/
681
759
.. _`ZooKeeper` : https://zookeeper.apache.org/
0 commit comments