@@ -169,6 +169,7 @@ Store Scope Blocking Expiring
169
169
============================================ ====== ======== ========
170
170
:ref: `FlockStore <lock-store-flock >` local yes no
171
171
:ref: `MemcachedStore <lock-store-memcached >` remote no yes
172
+ :ref: `MongoDbStore <lock-store-mongodb >` remote no yes
172
173
:ref: `PdoStore <lock-store-pdo >` remote no yes
173
174
:ref: `RedisStore <lock-store-redis >` remote no yes
174
175
:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
@@ -215,6 +216,38 @@ support blocking, and expects a TTL to avoid stalled locks::
215
216
216
217
Memcached does not support TTL lower than 1 second.
217
218
219
+ .. _lock-store-mongodb :
220
+
221
+ MongoDbStore
222
+ ~~~~~~~~~~~~
223
+
224
+ .. versionadded :: 4.3
225
+ The MongoDbStore was introduced Symfony 4.3.
226
+
227
+ The MongoDbStore saves locks on a MongoDB server, it requires a ``\MongoDB\Client ``
228
+ connection from `mongodb/mongodb `_.
229
+ This store does not support blocking and expects a TTL to avoid stalled locks::
230
+
231
+ use Symfony\Component\Lock\Store\MongoDbStore;
232
+
233
+ $mongoClient = new \MongoDB\Client('mongo://localhost/');
234
+
235
+ $options = array(
236
+ 'database' => 'my-app',
237
+ );
238
+
239
+ $store = new MongoDbStore($mongoClient, $options);
240
+
241
+ The ``MongoDbStore `` takes the following ``$options ``:
242
+
243
+ ============ ========= ========================================================================
244
+ Option Default Description
245
+ ============ ========= ========================================================================
246
+ database The name of the database [Mandatory]
247
+ collection ``lock `` The name of the collection
248
+ gcProbablity ``0.001 `` Should a TTL Index be created expressed as a probability from 0.0 to 1.0
249
+ ============ ========= ========================================================================
250
+
218
251
.. _lock-store-pdo :
219
252
220
253
PdoStore
@@ -351,7 +384,8 @@ Remote Stores
351
384
~~~~~~~~~~~~~
352
385
353
386
Remote stores (:ref: `MemcachedStore <lock-store-memcached >`,
354
- :ref: `PdoStore <lock-store-pdo >`, :ref: `RedisStore <lock-store-redis >`, and
387
+ :ref: `MongoDbStore <lock-store-mongodb >`, :ref: `PdoStore <lock-store-pdo >`,
388
+ :ref: `RedisStore <lock-store-redis >` and
355
389
:ref: `ZookeeperStore <lock-store-zookeeper >`) use a unique token to recognize
356
390
the true owner of the lock. This token is stored in the
357
391
:class: `Symfony\\ Component\\ Lock\\ Key ` object and is used internally by
@@ -375,7 +409,8 @@ Expiring Stores
375
409
~~~~~~~~~~~~~~~
376
410
377
411
Expiring stores (:ref: `MemcachedStore <lock-store-memcached >`,
378
- :ref: `PdoStore <lock-store-pdo >` and :ref: `RedisStore <lock-store-redis >`)
412
+ :ref: `MongoDbStore <lock-store-mongodb >`, :ref: `PdoStore <lock-store-pdo >` and
413
+ :ref: `RedisStore <lock-store-redis >`)
379
414
guarantee that the lock is acquired only for the defined duration of time. If
380
415
the task takes longer to be accomplished, then the lock can be released by the
381
416
store and acquired by someone else.
@@ -492,6 +527,54 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
492
527
The method ``flush() `` must not be called, or locks should be stored in a
493
528
dedicated Memcached service away from Cache.
494
529
530
+ MongoDbStore
531
+ ~~~~~~~~~~~~
532
+
533
+ .. caution ::
534
+
535
+ The locked resouce name is indexed in the ``_id `` field of the
536
+ lock collection.
537
+ An indexed field's value in MongoDB can be a maximum of 1024 bytes in
538
+ length inclusive of structural overhead.
539
+
540
+ For more details see: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
541
+
542
+ A TTL index MUST BE used on MongoDB 2.2+ to automatically clean up expired locks.
543
+
544
+ Such an index can be created manually:
545
+
546
+ .. code-block :: javascript
547
+
548
+ db .lock .ensureIndex (
549
+ { " expires_at" : 1 },
550
+ { " expireAfterSeconds" : 0 }
551
+ )
552
+
553
+ Alternatively, the method ``MongoDbStore::createTtlIndex(int $expireAfterSeconds = 0) ``
554
+ can be called once to create the TTL index during database setup.
555
+
556
+ For more details see: http://docs.mongodb.org/manual/tutorial/expire-data/
557
+
558
+ .. tip ::
559
+
560
+ ``MongoDbStore `` will attempt to automatically create a TTL index on
561
+ mongodb 2.2+. It's recommended to set constructor option
562
+ ``gcProbablity = 0.0 `` to disable this behaviour if you have manually
563
+ dealt with TTL index creation.
564
+
565
+ .. caution ::
566
+
567
+ This store relies on all php application and database nodes to have
568
+ synchronized clocks for lock expiry to occur at the correct time.
569
+ To ensure locks don't expire prematurely; the lock TTL should be set
570
+ with enough extra time in ``expireAfterSeconds `` to account for any
571
+ clock drift between nodes.
572
+
573
+ ``writeConcern ``, ``readConcern `` and ``readPreference `` are not specified by
574
+ MongoDbStore meaning the collection's settings will take effect.
575
+
576
+ For more details see: https://docs.mongodb.com/manual/applications/replication/
577
+
495
578
PdoStore
496
579
~~~~~~~~~~
497
580
@@ -612,6 +695,7 @@ are still running.
612
695
613
696
.. _`ACID` : https://en.wikipedia.org/wiki/ACID
614
697
.. _`locks` : https://en.wikipedia.org/wiki/Lock_(computer_science)
698
+ .. _`mongodb/mongodb` : https://packagist.org/packages/mongodb/mongodb
615
699
.. _Packagist : https://packagist.org/packages/symfony/lock
616
700
.. _`PHP semaphore functions` : http://php.net/manual/en/book.sem.php
617
701
.. _`PDO` : https://php.net/pdo
0 commit comments