/** * Setup performed prior to each test method * * @return void */ public function setUp() { if (!extension_loaded('mongo')) { $this->markTestSkipped('Zend\\Session\\SaveHandler\\MongoDB tests are not enabled due to missing Mongo extension'); } $this->options = new MongoDBOptions(array('database' => 'zf2_tests', 'collection' => 'sessions')); $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\\Mongo' : '\\MongoClient'; $this->mongo = new $mongoClass(); $this->mongoCollection = $this->mongo->selectCollection($this->options->getDatabase(), $this->options->getCollection()); }
public function testSetters() { $options = new MongoDBOptions(); $options->setDatabase('testDatabase')->setCollection('testCollection')->setSaveOptions(array('safe' => 2))->setNameField('testName')->setDataField('testData')->setLifetimeField('testLifetime')->setModifiedField('testModified'); $this->assertEquals('testDatabase', $options->getDatabase()); $this->assertEquals('testCollection', $options->getCollection()); $this->assertEquals(array('safe' => 2), $options->getSaveOptions()); $this->assertEquals('testName', $options->getNameField()); $this->assertEquals('testData', $options->getDataField()); $this->assertEquals('testLifetime', $options->getLifetimeField()); $this->assertEquals('testModified', $options->getModifiedField()); }
/** * Garbage collection * * Note: MongoDB 2.2+ supports TTL collections, which may be used in place * of this method by indexing the "modified" 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/ * @param int $maxlifetime * @return bool */ public function gc($maxlifetime) { /* Note: unlike DbTableGateway, we do not use the lifetime field in * each document. Doing so would require a $where query to work with the * computed value (modified + lifetime) and be very inefficient. */ $result = $this->mongoCollection->remove(array($this->options->getModifiedField() => array('$lt' => new MongoDate(time() - $maxlifetime))), $this->options->getSaveOptions()); return (bool) (isset($result['ok']) ? $result['ok'] : $result); }
/** * Garbage collection * * Note: MongoDB 2.2+ supports TTL collections, which may be used in place * of this method by indexing the "modified" 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/ * @param int $maxlifetime * @return bool */ public function gc($maxlifetime) { /* Note: unlike DbTableGateway, we do not use the lifetime field in * each document. Doing so would require a $where query to work with the * computed value (modified + lifetime) and be very inefficient. */ $microseconds = floor(microtime(true) * 1000) - $maxlifetime; $result = $this->mongoCollection->deleteMany([$this->options->getModifiedField() => ['$lt' => new UTCDateTime($microseconds)]], $this->options->getSaveOptions()); return $result->isAcknowledged(); }