laravel-mongodb-session icon indicating copy to clipboard operation
laravel-mongodb-session copied to clipboard

Does the package use mongdbs TTL functionality?

Open Evanion opened this issue 10 years ago • 2 comments

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?

Evanion avatar Mar 04 '14 19:03 Evanion

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

jenssegers avatar Mar 04 '14 22:03 jenssegers

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?

Evanion avatar Mar 05 '14 01:03 Evanion