@@ -173,6 +173,7 @@ Store Scope Blocking Expiring
173
173
============================================ ====== ======== ========
174
174
:ref: `FlockStore <lock-store-flock >` local yes no
175
175
:ref: `MemcachedStore <lock-store-memcached >` remote no yes
176
+ :ref: `MongoDbStore <lock-store-mongodb >` remote no yes
176
177
:ref: `PdoStore <lock-store-pdo >` remote no yes
177
178
:ref: `RedisStore <lock-store-redis >` remote no yes
178
179
:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
@@ -219,6 +220,36 @@ support blocking, and expects a TTL to avoid stalled locks::
219
220
220
221
Memcached does not support TTL lower than 1 second.
221
222
223
+ .. _lock-store-mongodb :
224
+
225
+ MongoDbStore
226
+ ~~~~~~~~~~~~
227
+
228
+ .. versionadded :: 4.2
229
+ The MongoDbStore was introduced Symfony 4.2.
230
+
231
+ The MongoDbStore saves locks on a MongoDB server, it requires a ``\MongoDB\Client ``
232
+ connection from `mongodb/mongodb `_.
233
+ This store does not support blocking and expects a TTL to avoid stalled locks::
234
+
235
+ use Symfony\Component\Lock\Store\MongoDbStore;
236
+
237
+ $mongoClient = new \MongoDB\Client('mongo://localhost/');
238
+
239
+ $options = array(
240
+ 'database' => 'my-app',
241
+ );
242
+
243
+ $store = new MongoDbStore($mongoClient, $options);
244
+
245
+ The ``MongoDbStore `` takes the following ``$options ``:
246
+
247
+ Option Default Description
248
+ ========== ======== ====================================
249
+ database The name of the database [Mandatory]
250
+ collection ``lock `` The name of the collection
251
+ ========== ======== ====================================
252
+
222
253
.. _lock-store-pdo :
223
254
224
255
PdoStore
@@ -361,7 +392,8 @@ Remote Stores
361
392
~~~~~~~~~~~~~
362
393
363
394
Remote stores (:ref: `MemcachedStore <lock-store-memcached >`,
364
- :ref: `PdoStore <lock-store-pdo >`, :ref: `RedisStore <lock-store-redis >`, and
395
+ :ref: `MongoDbStore <lock-store-mongodb >`, :ref: `PdoStore <lock-store-pdo >`,
396
+ :ref: `RedisStore <lock-store-redis >` and
365
397
:ref: `ZookeeperStore <lock-store-zookeeper >`) use a unique token to recognize
366
398
the true owner of the lock. This token is stored in the
367
399
:class: `Symfony\\ Component\\ Lock\\ Key ` object and is used internally by
@@ -385,7 +417,8 @@ Expiring Stores
385
417
~~~~~~~~~~~~~~~
386
418
387
419
Expiring stores (:ref: `MemcachedStore <lock-store-memcached >`,
388
- :ref: `PdoStore <lock-store-pdo >` and :ref: `RedisStore <lock-store-redis >`)
420
+ :ref: `MongoDbStore <lock-store-mongodb >`, :ref: `PdoStore <lock-store-pdo >` and
421
+ :ref: `RedisStore <lock-store-redis >`)
389
422
guarantee that the lock is acquired only for the defined duration of time. If
390
423
the task takes longer to be accomplished, then the lock can be released by the
391
424
store and acquired by someone else.
@@ -502,6 +535,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
502
535
The method ``flush() `` must not be called, or locks should be stored in a
503
536
dedicated Memcached service away from Cache.
504
537
538
+ MongoDbStore
539
+ ~~~~~~~~~~~~
540
+
541
+ .. caution ::
542
+
543
+ The locked resouce name is indexed in the ``_id `` field of the
544
+ lock collection.
545
+ An indexed field's value in MongoDB can be a maximum of 1024 bytes in
546
+ length inclusive of structural overhead.
547
+
548
+ For more details see: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
549
+
550
+ A TTL index MUST BE used on MongoDB 2.2+ to automatically clean up expired locks.
551
+
552
+ Such an index can be created manually:
553
+
554
+ .. code-block :: javascript
555
+
556
+ db .lock .ensureIndex (
557
+ { " expires_at" : 1 },
558
+ { " expireAfterSeconds" : 0 }
559
+ )
560
+
561
+ Alternatively, the method ``MongoDbStore::createTtlIndex(int $expireAfterSeconds = 0) ``
562
+ can be called once to create the TTL index during database setup.
563
+
564
+ For more details see: http://docs.mongodb.org/manual/tutorial/expire-data/
565
+
566
+ .. caution ::
567
+
568
+ This store relies on all client and server nodes to have
569
+ synchronized clocks for lock expiry to occur at the correct time.
570
+ To ensure locks don't expire prematurely; the lock TTL should be set
571
+ with enough extra time to account for any 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
+
505
578
PdoStore
506
579
~~~~~~~~~~
507
580
@@ -622,6 +695,7 @@ are still running.
622
695
623
696
.. _`ACID` : https://en.wikipedia.org/wiki/ACID
624
697
.. _`locks` : https://en.wikipedia.org/wiki/Lock_(computer_science)
698
+ .. _`mongodb/mongodb` : https://packagist.org/packages/mongodb/mongodb
625
699
.. _Packagist : https://packagist.org/packages/symfony/lock
626
700
.. _`PHP semaphore functions` : http://php.net/manual/en/book.sem.php
627
701
.. _`PDO` : https://php.net/pdo
0 commit comments