jenssegers / laravel-mongodb-session

A MongoDB session driver for Laravel
77 stars 27 forks source link

Does the package use mongdbs TTL functionality? #4

Open Evanion opened 10 years ago

Evanion commented 10 years ago

Hello I was just wondering if the package implements the automatic TTL function in MongoDB?

I noticed that the package adds the last_activity field to the documents. What defines the TTL? is it the session lifetime configuration in Laravels session.php?

jenssegers commented 10 years ago

I don't really know, it's using Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler

Evanion commented 10 years ago

Looking at the code (both in my Laravel package and the repo on gh), it does contain a function to support TTL:

    public function gc($lifetime)
    {
        /* Note: MongoDB 2.2+ supports TTL collections, which may be used in
         * place of this method by indexing the "time_field" field with an
         * "expireAfterSeconds" option. Regardless of whether TTL collections
         * are used, consider indexing this field to make the remove query more
         * efficient.
         *
         * See: http://docs.mongodb.org/manual/tutorial/expire-data/
         */
        $time = new \MongoDate(time() - $lifetime);

        $this->getCollection()->remove(array(
            $this->options['time_field'] => array('$lt' => $time),
        ));

        return true;
    }

https://github.com/symfony/HttpFoundation/blob/master/Session/Storage/Handler/MongoDbSessionHandler.php#L102

But looking at the system.indexes collection of the database, it doesn't seem like the function is used, since, as I understand it, you need to define the 'last_activity' field as a TTL index (I'm really new to mdb, and my expertise is frontend), and a TTL collection can't use a _id index http://docs.mongodb.org/manual/core/index-ttl/

How difficult would it be to add TTL support?