Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f48e8a

Browse filesBrowse files
author
Joe Bennett
committed
#27345 Added Component\Lock\Store\MongoDbStore
1 parent d2907db commit 2f48e8a
Copy full SHA for 2f48e8a

File tree

1 file changed

+76
-2
lines changed
Filter options

1 file changed

+76
-2
lines changed

‎components/lock.rst

Copy file name to clipboardExpand all lines: components/lock.rst
+76-2Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

224255
PdoStore
@@ -361,7 +392,8 @@ Remote Stores
361392
~~~~~~~~~~~~~
362393

363394
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
365397
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
366398
the 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

387419
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>`)
389422
guarantee that the lock is acquired only for the defined duration of time. If
390423
the task takes longer to be accomplished, then the lock can be released by the
391424
store 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+
505578
PdoStore
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.