@@ -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: `RedisStore <lock-store-redis >` remote no yes
177
178
:ref: `SemaphoreStore <lock-store-semaphore >` local yes no
178
179
============================================ ====== ======== ========
@@ -217,6 +218,37 @@ support blocking, and expects a TTL to avoid stalled locks::
217
218
218
219
Memcached does not support TTL lower than 1 second.
219
220
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
+
220
252
.. _lock-store-redis :
221
253
222
254
RedisStore
@@ -290,7 +322,8 @@ the component is used in the following way.
290
322
Remote Stores
291
323
~~~~~~~~~~~~~
292
324
293
- Remote stores (:ref: `MemcachedStore <lock-store-memcached >` and
325
+ Remote stores (:ref: `MemcachedStore <lock-store-memcached >`,
326
+ :ref: `MongoDbStore <lock-store-mongodb >` and
294
327
:ref: `RedisStore <lock-store-redis >`) use an unique token to recognize the true
295
328
owner of the lock. This token is stored in the
296
329
: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`
313
346
Expiring Stores
314
347
~~~~~~~~~~~~~~~
315
348
316
- Expiring stores (:ref: `MemcachedStore <lock-store-memcached >` and
349
+ Expiring stores (:ref: `MemcachedStore <lock-store-memcached >`,
350
+ :ref: `MongoDbStore <lock-store-mongodb >` and
317
351
:ref: `RedisStore <lock-store-redis >`) guarantee that the lock is acquired
318
352
only for the defined duration of time. If the task takes longer to be
319
353
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
431
465
The method ``flush() `` must not be called, or locks should be stored in a
432
466
dedicated Memcached service away from Cache.
433
467
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
+
434
490
RedisStore
435
491
~~~~~~~~~~
436
492
0 commit comments