arangodb / arangodb-php

PHP ODM for ArangoDB
https://www.arangodb.com
Apache License 2.0
182 stars 46 forks source link

Activate AQL query cache #183

Closed helderjs closed 9 years ago

helderjs commented 9 years ago

How can I turn on the query cache when performing a statment? I found this (https://docs.arangodb.com/devel/Aql/QueryCache.html) in the documentation but i can't do that using the php driver.

jsteemann commented 9 years ago

Not yet. The query cache is only contained in the devel branch, not in a stable branch or release. That automatically means it is subject to changes and the API can be considered unstable. When we create the next release branch (2.7) from devel, there will probably be support for the cache in the PHP driver, provided that the cache feature makes it into the release.

jsteemann commented 9 years ago

We should leave this issue open until 2.7 release in order to re-check then.

jsteemann commented 9 years ago

The PHP driver now supports the AQL query cache. Here is some example code that works in the devel branch. A dedicated 2.7 branch for the driver is about to appear, too.

$cacheHandler = new QueryCacheHandler($this->connection);
$cacheHandler->enableDemandMode(); // turn query cache on only for queries that have the `cache` attribute set in their options

$query = "FOR doc IN collection FILTER doc.value == 42 RETURN doc"; // example AQL query

// enable query result cache option. this will make the server consult the AQL query result cache for this query
$statement = new Statement($this->connection, array("cache" => true)); 
$statement->setQuery($query);
$cursor = $statement->execute();

// whether or not query result was served from AQL query result cache
var_dump($cursor->getCached()); 

// re-execute same query
$statement = new Statement($this->connection, array("cache" => true)); 
$statement->setQuery($query);
$cursor = $statement->execute();

// should be served from the cache now, provided the underlying collection wasn't changed
var_dump($cursor->getCached()); 

The query cache can also be globally turned on or off programmatically with the PHP driver, or set to demand mode as above:

$cacheHandler->enable();
$cacheHandler->disable();
$cacheHandler->enableDemandMode();

The demand mode allows per-query control.

eafiontzi commented 6 years ago

Hello,

I am trying to enable AQL query cache for my application, but cannot seem to find exactly how to. I am running arangoDB 3.1.27 and a query looks like this:

    // create a statement to insert 1000 test users
    $statement = new ArangoStatement(
        $connection, [
                       'query' => 'FOR i IN 1..1000 INSERT { _key: CONCAT("test", i) } IN users'
                   ]
    );

    // execute the statement
    $cursor = $statement->execute();

so it is a bit different than the one shown above. Should the "array("cache" => true)" be a parameter in the above statement?

In addition, where should these two lines go?

$cacheHandler = new QueryCacheHandler($this->connection);
$cacheHandler->enableDemandMode(); // turn query cache on only for queries that have the `cache` attribute set in their options

Could they be added somehow in the configuration of the ArangoDB connection?

   protected function createConnectionInstance()
    {
        // set up some basic connection options
        $this->connectionOptions = [
            ArangoConnectionOptions::OPTION_ENDPOINT => $this->endpoint,
            // authorization type to use (currently supported: 'Basic')
            ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
            // user for basic authorization
            ArangoConnectionOptions::OPTION_AUTH_USER => $this->username,
            // password for basic authorization
            ArangoConnectionOptions::OPTION_AUTH_PASSWD => $this->password,
            // connection persistence on server.
            // can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
            ArangoConnectionOptions::OPTION_CONNECTION => $this->persistence,
            // connect timeout in seconds
            ArangoConnectionOptions::OPTION_TIMEOUT => $this->timeout,
            // optionally create new collections when inserting documents
            ArangoConnectionOptions::OPTION_CREATE => $this->autoCreateCollection,
            // When updating a document that was previously/concurrently updated by another user
            ArangoConnectionOptions::OPTION_UPDATE_POLICY => $this->updatePolicy,
            // the database name
            ArangoConnectionOptions::OPTION_DATABASE => $this->database,
        ];
        return new ArangoConnection($this->connectionOptions);
    }

Thanks in advance