@@ -216,6 +216,7 @@ Store Scope Blocking Expiring
216
216
============================================ ====== ======== ========
217
217
:ref: `FlockStore <lock-store-flock >` local yes no
218
218
:ref: `MemcachedStore <lock-store-memcached >` remote no yes
219
+ :ref: `MongoDbStore <lock-store-mongodb >` remote no yes
219
220
:ref: `PdoStore <lock-store-pdo >` remote no yes
220
221
:ref: `RedisStore <lock-store-redis >` remote no yes
221
222
:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
@@ -262,6 +263,39 @@ support blocking, and expects a TTL to avoid stalled locks::
262
263
263
264
Memcached does not support TTL lower than 1 second.
264
265
266
+ .. _lock-store-mongodb :
267
+
268
+ MongoDbStore
269
+ ~~~~~~~~~~~~
270
+
271
+ .. versionadded :: 4.4
272
+
273
+ The ``MongoDbStore `` was introduced in Symfony 4.4.
274
+
275
+ The MongoDbStore saves locks on a MongoDB server, it requires a
276
+ ``\MongoDB\Client `` connection from `mongodb/mongodb `_. This store does not
277
+ support blocking and expects a TTL to avoid stalled locks::
278
+
279
+ use Symfony\Component\Lock\Store\MongoDbStore;
280
+
281
+ $mongoClient = new \MongoDB\Client('mongo://localhost/');
282
+
283
+ $options = [
284
+ 'database' => 'my-app',
285
+ ];
286
+
287
+ $store = new MongoDbStore($mongoClient, $options);
288
+
289
+ The ``MongoDbStore `` takes the following ``$options ``:
290
+
291
+ ============ ========= ========================================================================
292
+ Option Default Description
293
+ ============ ========= ========================================================================
294
+ database The name of the database [Mandatory]
295
+ collection ``lock `` The name of the collection
296
+ gcProbablity ``0.001 `` Should a TTL Index be created expressed as a probability from 0.0 to 1.0
297
+ ============ ========= ========================================================================
298
+
265
299
.. _lock-store-pdo :
266
300
267
301
PdoStore
@@ -398,7 +432,7 @@ Remote Stores
398
432
~~~~~~~~~~~~~
399
433
400
434
Remote stores (:ref: `MemcachedStore <lock-store-memcached >`,
401
- :ref: `PdoStore <lock-store-pdo >`,
435
+ :ref: `MongoDbStore < lock-store-mongodb >`, :ref: ` PdoStore <lock-store-pdo >`,
402
436
:ref: `RedisStore <lock-store-redis >` and
403
437
:ref: `ZookeeperStore <lock-store-zookeeper >`) use a unique token to recognize
404
438
the true owner of the lock. This token is stored in the
@@ -423,7 +457,7 @@ Expiring Stores
423
457
~~~~~~~~~~~~~~~
424
458
425
459
Expiring stores (:ref: `MemcachedStore <lock-store-memcached >`,
426
- :ref: `PdoStore <lock-store-pdo >` and
460
+ :ref: `MongoDbStore < lock-store-mongodb >`, :ref: ` PdoStore <lock-store-pdo >` and
427
461
:ref: `RedisStore <lock-store-redis >`)
428
462
guarantee that the lock is acquired only for the defined duration of time. If
429
463
the task takes longer to be accomplished, then the lock can be released by the
@@ -541,6 +575,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
541
575
The method ``flush() `` must not be called, or locks should be stored in a
542
576
dedicated Memcached service away from Cache.
543
577
578
+ MongoDbStore
579
+ ~~~~~~~~~~~~
580
+
581
+ .. caution ::
582
+
583
+ The locked resource name is indexed in the ``_id `` field of the lock
584
+ collection. Beware that in MongoDB an indexed field's value can be
585
+ `a maximum of 1024 bytes in length `_ inclusive of structural overhead.
586
+
587
+ A TTL index MUST BE used on MongoDB 2.2+ to automatically clean up expired locks.
588
+ Such an index can be created manually:
589
+
590
+ .. code-block :: javascript
591
+
592
+ db .lock .ensureIndex (
593
+ { " expires_at" : 1 },
594
+ { " expireAfterSeconds" : 0 }
595
+ )
596
+
597
+ Alternatively, the method ``MongoDbStore::createTtlIndex(int $expireAfterSeconds = 0) ``
598
+ can be called once to create the TTL index during database setup. Read more
599
+ about `Expire Data from Collections by Setting TTL `_ in MongoDB.
600
+
601
+ .. tip ::
602
+
603
+ ``MongoDbStore `` will attempt to automatically create a TTL index on MongoDB
604
+ 2.2+. It's recommended to set constructor option ``gcProbablity = 0.0 `` to
605
+ disable this behavior if you have manually dealt with TTL index creation.
606
+
607
+ .. caution ::
608
+
609
+ This store relies on all PHP application and database nodes to have
610
+ synchronized clocks for lock expiry to occur at the correct time. To ensure
611
+ locks don't expire prematurely; the lock TTL should be set with enough extra
612
+ time in ``expireAfterSeconds `` to account for any clock drift between nodes.
613
+
614
+ ``writeConcern ``, ``readConcern `` and ``readPreference `` are not specified by
615
+ MongoDbStore meaning the collection's settings will take effect. Read more
616
+ about `Replica Set Read and Write Semantics `_ in MongoDB.
617
+
544
618
PdoStore
545
619
~~~~~~~~~~
546
620
@@ -661,9 +735,13 @@ are still running.
661
735
662
736
.. _`ACID` : https://en.wikipedia.org/wiki/ACID
663
737
.. _`locks` : https://en.wikipedia.org/wiki/Lock_(computer_science)
738
+ .. _`mongodb/mongodb` : https://packagist.org/packages/mongodb/mongodb
664
739
.. _Packagist : https://packagist.org/packages/symfony/lock
665
740
.. _`PHP semaphore functions` : http://php.net/manual/en/book.sem.php
666
741
.. _`PDO` : https://php.net/pdo
667
742
.. _`Doctrine DBAL Connection` : https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
668
743
.. _`Data Source Name (DSN)` : https://en.wikipedia.org/wiki/Data_source_name
669
744
.. _`ZooKeeper` : https://zookeeper.apache.org/
745
+ .. _`a maximum of 1024 bytes in length` : https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
746
+ .. _`Expire Data from Collections by Setting TTL` : https://docs.mongodb.com/manual/tutorial/expire-data/
747
+ .. _`Replica Set Read and Write Semantics` : https://docs.mongodb.com/manual/applications/replication/
0 commit comments