You are here

public function MongoDbSessionHandler::__construct in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-foundation/Session/Storage/Handler/MongoDbSessionHandler.php \Symfony\Component\HttpFoundation\Session\Storage\Handler\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]

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>.ensureIndex( { "<expiry-field>": 1 }, { "expireAfterSeconds": 0 } )

More details on: http://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.

Parameters

\Mongo|\MongoClient $mongo A MongoClient or Mongo instance:

array $options An associative array of field options:

Throws

\InvalidArgumentException When MongoClient or Mongo instance not provided

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

File

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

Class

MongoDbSessionHandler
MongoDB session handler.

Namespace

Symfony\Component\HttpFoundation\Session\Storage\Handler

Code

public function __construct($mongo, array $options) {
  if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
    throw new \InvalidArgumentException('MongoClient or Mongo instance required');
  }
  if (!isset($options['database']) || !isset($options['collection'])) {
    throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
  }
  $this->mongo = $mongo;
  $this->options = array_merge(array(
    'id_field' => '_id',
    'data_field' => 'data',
    'time_field' => 'time',
    'expiry_field' => 'expires_at',
  ), $options);
}