Skip to content

Commit a2b0a13

Browse files
author
Joe Bennett
committed
#27345 Added MongoDbStore Lock Store documentation
1 parent 40b2067 commit a2b0a13

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

components/lock.rst

Lines changed: 58 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:`RedisStore <lock-store-redis>` remote no yes
177178
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
178179
============================================ ====== ======== ========
@@ -217,6 +218,37 @@ support blocking, and expects a TTL to avoid stalled locks::
217218

218219
Memcached does not support TTL lower than 1 second.
219220

221+
.. _lock-store-mongodb:
222+
223+
MongoDbStore
224+
~~~~~~~~~~
225+
226+
The MongoDbStore saves locks on a MongoDB server, it requires a ``\MongoDB\Client`` connection.
227+
This store does not support blocking, and expects a TTL to avoid stalled locks::
228+
229+
use Symfony\Component\Lock\Store\MongoDbStore;
230+
231+
$mongoClient = new \MongoDB\Client('mongo://localhost/');
232+
233+
$options = array(
234+
'database' => 'my-app',
235+
);
236+
237+
$store = new MongoDbStore($mongoClient, $options);
238+
239+
The ``MongoDbStore`` takes the following ``$options``:
240+
241+
============== ========= ============== ===================================================================================
242+
Option Mandatory Default Description
243+
============== ========= ============== ===================================================================================
244+
database Yes The name of the database
245+
collection No ``lock`` The name of the collection
246+
resource_field No ``_id`` The field name for storing the lock id **MUST be uniquely indexed if you chage it**
247+
token_field No ``token`` The field name for storing the lock token
248+
aquired_field No ``aquired_at`` The field name for storing the acquisition timestamp
249+
expiry_field No ``expires_at`` The field name for storing the expiry-timestamp
250+
============== ========= ============== ===================================================================================
251+
220252
.. _lock-store-redis:
221253

222254
RedisStore
@@ -290,7 +322,8 @@ the component is used in the following way.
290322
Remote Stores
291323
~~~~~~~~~~~~~
292324

293-
Remote stores (:ref:`MemcachedStore <lock-store-memcached>` and
325+
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
326+
:ref:`MongoDbStore <lock-store-mongodb>` and
294327
:ref:`RedisStore <lock-store-redis>`) use an unique token to recognize the true
295328
owner of the lock. This token is stored in the
296329
:class:`Symfony\\Component\\Lock\\Key` object and is used internally by the
@@ -313,7 +346,8 @@ different machines may allow two different processes to acquire the same ``Lock`
313346
Expiring Stores
314347
~~~~~~~~~~~~~~~
315348

316-
Expiring stores (:ref:`MemcachedStore <lock-store-memcached>` and
349+
Expiring stores (:ref:`MemcachedStore <lock-store-memcached>`,
350+
:ref:`MongoDbStore <lock-store-mongodb>` and
317351
:ref:`RedisStore <lock-store-redis>`) guarantee that the lock is acquired
318352
only for the defined duration of time. If the task takes longer to be
319353
accomplished, then the lock can be released by the store and acquired by
@@ -431,6 +465,28 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
431465
The method ``flush()`` must not be called, or locks should be stored in a
432466
dedicated Memcached service away from Cache.
433467

468+
MongoDbStore
469+
~~~~~~~~~~~~~~
470+
471+
The MongoDbStore relies on a unique index on the ``<resource_field>``. By default
472+
this is ``_id`` which already has a unique index being the collection identifier.
473+
It is strongly recommended to leave this as is. If you do need to change the
474+
``<resource_field>`` you **MUST** ensure you place a unique index on your filed.
475+
476+
It's possible to automatically expire the locks in the database as described below:
477+
478+
A TTL index can be used on MongoDB 2.2+ to cleanup expired locks
479+
automatically. Such an index can for example look like this:
480+
481+
.. code-block:: javascript
482+
483+
db.<lock-collection>.ensureIndex(
484+
{ "<expiry-field>": 1 },
485+
{ "expireAfterSeconds": 0 }
486+
)
487+
488+
For more details see: http://docs.mongodb.org/manual/tutorial/expire-data/
489+
434490
RedisStore
435491
~~~~~~~~~~
436492

0 commit comments

Comments
 (0)