Skip to content

Commit 9c96209

Browse files
committed
minor #9807 #27345 Added MongoDbStore Lock Store documentation (Joe Bennett)
This PR was merged into the master branch. Discussion ---------- #27345 Added MongoDbStore Lock Store documentation Added support for MongoDbStore Lock Store, See symfony/symfony#27648 Commits ------- a641baa #27345 Added docs for Symfony\Component\Lock\Store\MongoDbStore
2 parents 7bacdf5 + a641baa commit 9c96209

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

components/lock.rst

+86-2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Store Scope Blocking Expiring
169169
============================================ ====== ======== ========
170170
:ref:`FlockStore <lock-store-flock>` local yes no
171171
:ref:`MemcachedStore <lock-store-memcached>` remote no yes
172+
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes
172173
:ref:`PdoStore <lock-store-pdo>` remote no yes
173174
:ref:`RedisStore <lock-store-redis>` remote no yes
174175
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
@@ -215,6 +216,38 @@ support blocking, and expects a TTL to avoid stalled locks::
215216

216217
Memcached does not support TTL lower than 1 second.
217218

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+
218251
.. _lock-store-pdo:
219252

220253
PdoStore
@@ -351,7 +384,8 @@ Remote Stores
351384
~~~~~~~~~~~~~
352385

353386
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
355389
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
356390
the true owner of the lock. This token is stored in the
357391
:class:`Symfony\\Component\\Lock\\Key` object and is used internally by
@@ -375,7 +409,8 @@ Expiring Stores
375409
~~~~~~~~~~~~~~~
376410

377411
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>`)
379414
guarantee that the lock is acquired only for the defined duration of time. If
380415
the task takes longer to be accomplished, then the lock can be released by the
381416
store and acquired by someone else.
@@ -492,6 +527,54 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
492527
The method ``flush()`` must not be called, or locks should be stored in a
493528
dedicated Memcached service away from Cache.
494529

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+
495578
PdoStore
496579
~~~~~~~~~~
497580

@@ -612,6 +695,7 @@ are still running.
612695

613696
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
614697
.. _`locks`: https://en.wikipedia.org/wiki/Lock_(computer_science)
698+
.. _`mongodb/mongodb`: https://packagist.org/packages/mongodb/mongodb
615699
.. _Packagist: https://packagist.org/packages/symfony/lock
616700
.. _`PHP semaphore functions`: http://php.net/manual/en/book.sem.php
617701
.. _`PDO`: https://php.net/pdo

0 commit comments

Comments
 (0)