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

Breadcrumb

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

function MongoDbSessionHandler::__construct

Constructor.

List of available options:

  • database: The name of the database [required]
  • collection: The name of the collection [required]
  • id_field: The field name for storing the session id [default: _id]
  • data_field: The field name for storing the session data [default: data]
  • time_field: The field name for storing the timestamp [default: time]
  • expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
  • ttl: The time to live in seconds.

It is strongly recommended to put an index on the `expiry_field` for garbage-collection. Alternatively it's possible to automatically expire the sessions in the database as described below:

A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions automatically. Such an index can for example look like this:

db.<session-collection>.createIndex( { "<expiry-field>": 1 }, { "expireAfterSeconds": 0 } )

More details on: https://docs.mongodb.org/manual/tutorial/expire-data/

If you use such an index, you can drop `gc_probability` to 0 since no garbage-collection is required.

Throws

\InvalidArgumentException When "database" or "collection" not provided

File

vendor/symfony/http-foundation/Session/Storage/Handler/MongoDbSessionHandler.php, line 67

Class

MongoDbSessionHandler
Session handler using the MongoDB driver extension.

Namespace

Symfony\Component\HttpFoundation\Session\Storage\Handler

Code

public function __construct(Client|Manager $mongo, array $options) {
    if (!isset($options['database']) || !isset($options['collection'])) {
        throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler.');
    }
    if ($mongo instanceof Client) {
        $mongo = $mongo->getManager();
    }
    $this->manager = $mongo;
    $this->namespace = $options['database'] . '.' . $options['collection'];
    $this->options = array_merge([
        'id_field' => '_id',
        'data_field' => 'data',
        'time_field' => 'time',
        'expiry_field' => 'expires_at',
    ], $options);
    $this->ttl = $this->options['ttl'] ?? null;
}

API Navigation

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