@@ -173,6 +173,7 @@ Store Scope Blocking Expiring
173173============================================ ====== ======== ========
174174:ref: `FlockStore <lock-store-flock >` local yes no
175175:ref: `MemcachedStore <lock-store-memcached >` remote no yes
176+ :ref: `MongoDbStore <lock-store-mongodb >` remote no yes
176177:ref: `PdoStore <lock-store-pdo >` remote no yes
177178:ref: `RedisStore <lock-store-redis >` remote no yes
178179:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
@@ -219,6 +220,36 @@ support blocking, and expects a TTL to avoid stalled locks::
219220
220221 Memcached does not support TTL lower than 1 second.
221222
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+
222253.. _lock-store-pdo :
223254
224255PdoStore
@@ -361,7 +392,8 @@ Remote Stores
361392~~~~~~~~~~~~~
362393
363394Remote 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
365397:ref: `ZookeeperStore <lock-store-zookeeper >`) use a unique token to recognize
366398the true owner of the lock. This token is stored in the
367399:class: `Symfony\\ Component\\ Lock\\ Key ` object and is used internally by
@@ -385,7 +417,8 @@ Expiring Stores
385417~~~~~~~~~~~~~~~
386418
387419Expiring 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 >`)
389422guarantee that the lock is acquired only for the defined duration of time. If
390423the task takes longer to be accomplished, then the lock can be released by the
391424store and acquired by someone else.
@@ -502,6 +535,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
502535 The method ``flush() `` must not be called, or locks should be stored in a
503536 dedicated Memcached service away from Cache.
504537
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+
505578PdoStore
506579~~~~~~~~~~
507580
@@ -622,6 +695,7 @@ are still running.
622695
623696.. _`ACID` : https://en.wikipedia.org/wiki/ACID
624697.. _`locks` : https://en.wikipedia.org/wiki/Lock_(computer_science)
698+ .. _`mongodb/mongodb` : https://packagist.org/packages/mongodb/mongodb
625699.. _Packagist : https://packagist.org/packages/symfony/lock
626700.. _`PHP semaphore functions` : http://php.net/manual/en/book.sem.php
627701.. _`PDO` : https://php.net/pdo
0 commit comments