Google\Cloud\Datastore\DatastoreSessionHandler::gc PHP Method

gc() public method

Delete the old session data from Cloud Datastore.
public gc ( $maxlifetime )
    public function gc($maxlifetime)
    {
        if ($this->gcLimit === 0) {
            return true;
        }
        try {
            $query = $this->datastore->query()->kind($this->kind)->filter('t', '<', time() - $maxlifetime)->order('t')->keysOnly()->limit($this->gcLimit);
            $result = $this->datastore->runQuery($query, ['namespaceId' => $this->namespaceId]);
            $keys = [];
            /* @var Entity $e */
            foreach ($result as $e) {
                $keys[] = $e->key();
            }
            if (!empty($keys)) {
                $this->datastore->deleteBatch($keys);
            }
        } catch (Exception $e) {
            trigger_error(sprintf('Session gc failed: %s', $e->getMessage()), E_USER_WARNING);
            return false;
        }
        return true;
    }

Usage Example

 /**
  * @expectedException PHPUnit_Framework_Error_Warning
  */
 public function testGcWithException()
 {
     $key1 = new Key('projectid');
     $key1->pathElement(self::KIND, 'sessionid1');
     $key2 = new Key('projectid');
     $key2->pathElement(self::KIND, 'sessionid2');
     $entity1 = new Entity($key1);
     $entity2 = new Entity($key2);
     $query = $this->prophesize(Query::class);
     $query->kind(self::KIND)->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $that = $this;
     $query->filter(Argument::type('string'), Argument::type('string'), Argument::type('int'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query) {
         $that->assertEquals('t', $args[0]);
         $that->assertEquals('<', $args[1]);
         $that->assertInternalType('int', $args[2]);
         $diff = time() - $args[2];
         // 2 seconds grace period should be enough
         $that->assertTrue($diff <= 102);
         $that->assertTrue($diff >= 100);
         return $query->reveal();
     });
     $query->order('t')->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $query->keysOnly()->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $query->limit(1000)->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $this->datastore->transaction()->shouldBeCalledTimes(1)->willReturn($this->transaction->reveal());
     $this->datastore->query()->shouldBeCalledTimes(1)->willReturn($query->reveal());
     $this->datastore->runQuery(Argument::type(Query::class), Argument::type('array'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query, $entity1, $entity2) {
         $that->assertEquals($query->reveal(), $args[0]);
         $that->assertEquals(['namespaceId' => self::NAMESPACE_ID], $args[1]);
         return [$entity1, $entity2];
     });
     $this->datastore->deleteBatch([$key1, $key2])->shouldBeCalledTimes(1)->willThrow(new Exception());
     $datastoreSessionHandler = new DatastoreSessionHandler($this->datastore->reveal(), 1000);
     $datastoreSessionHandler->open(self::NAMESPACE_ID, self::KIND);
     $ret = $datastoreSessionHandler->gc(100);
     $this->assertEquals(false, $ret);
 }