Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MongoDbStore.php

function MongoDbStore::createTtlIndex

Creates a TTL index to automatically remove expired locks.

If the gcProbability option is set higher than 0.0 (defaults to 0.001); there is a chance this will be called on self::save().

Otherwise; this should be called once manually during database setup.

Alternatively the TTL index can be created manually on the database:

db.lock.createIndex( { "expires_at": 1 }, { "expireAfterSeconds": 0 } )

Please note, expires_at is based on the application server. If the database time differs; a lock could be cleaned up before it has expired. To ensure locks don't expire prematurely; the lock TTL should be set with enough extra time to account for any clock drift between nodes.

A TTL index MUST BE used to automatically clean up expired locks.

Throws

UnsupportedException if options are not supported by the selected server

MongoInvalidArgumentException for parameter/option parsing errors

DriverRuntimeException for other driver errors (e.g. connection errors)

See also

http://docs.mongodb.org/manual/tutorial/expire-data/

1 call to MongoDbStore::createTtlIndex()
MongoDbStore::save in vendor/symfony/lock/Store/MongoDbStore.php

File

vendor/symfony/lock/Store/MongoDbStore.php, line 202

Class

MongoDbStore
MongoDbStore is a StoreInterface implementation using MongoDB as a storage engine. Support for MongoDB server >=2.2 due to use of TTL indexes.

Namespace

Symfony\Component\Lock\Store

Code

public function createTtlIndex(int $expireAfterSeconds = 0) : void {
    $server = $this->getManager()
        ->selectServer();
    $server->executeCommand($this->options['database'], new Command([
        'createIndexes' => $this->options['collection'],
        'indexes' => [
            [
                'key' => [
                    'expires_at' => 1,
                ],
                'name' => 'expires_at_1',
                'expireAfterSeconds' => $expireAfterSeconds,
            ],
        ],
    ]));
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal