Starting with 2.1 (2.2 for stable), MongoDB allows collections to have a Time To Live to be set with ensureIndex() to be automatically expired.

This could allow actual server based cleaning without any PHP code.


fgm’s picture

Come to think of it, some might even be interested in them for the watchdog: they are an alternative to capped collections in some scenarii.

fgm’s picture

Issue summary: View changes

The 2.1/2.2 implementation allowed only a single duration for a given index. Since 2.3/2.4 it is possible to specify expireAfterSeconds as 0, and insert the expiration timestamp in the documents themselves so the deletion thread will delete documents at their requested expiration, which seems just ideal.

fgm’s picture

After experimenting with it, using the latter option seems especially interesting, as it removes the need for an explicit flushing operation, which is performed by the MongoDB server in the background.

Some remarks before committing to this, though:

  • the expiration is performed by the TTLMonitor, which only runs every 60 seconds, so shorter expiration times will not be covered
  • the TTLMonitor can be disabled from the command line --setParameter ttlMonitorEnabled=false or by a command db.adminCommand({setParameter:1, ttlMonitorEnabled:false}), so its availability should be checked in the requirements

This does not seem to be an actual problem, though.

rhclayto’s picture

I'm actually doing this for sessions in a hacked version of the MongoDB Sessions module & it works great:

$expires_at = REQUEST_TIME + 200000;
  $fields = array(
    'uid' => (int) $user->uid,
    'cache' => isset($user->cache) ? (int) $user->cache : 0,
    'hostname' => ip_address(),
    'session' => $value,
    'timestamp' => REQUEST_TIME,
    'expires_at' => new MongoDate($expires_at),

It would be great to have this in the module. With this, I have ini_set('session.gc_probability', 0); in settings.php, i.e., no PHP garbage collection needed. +1

See also:!vendor!symfony!http-foundation!Se...

fgm’s picture

@rhclayto interested in rolling a patch for it ?