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