Skip to content

Commit 9b35428

Browse files
committed
Implemented GraphHandler cache for not making an API call for previously fetched results.
1 parent a01e8a0 commit 9b35428

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

lib/triagens/ArangoDb/GraphHandler.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ class GraphHandler extends
8585
*/
8686
const OPTION_EDGE_DEFINITIONS = 'edgeDefinitions';
8787

88-
/**
88+
/**
89+
* GraphHandler cache store
90+
*/
91+
protected $cache;
92+
93+
/**
8994
* Create a graph
9095
*
9196
* This will create a graph using the given graph object and return an array of the created graph object's attributes.<br><br>
@@ -348,7 +353,14 @@ public function deleteOrphanCollection($graph, $orphanCollection, $dropCollectio
348353
*
349354
*
350355
* @param mixed $graph - graph name as a string or instance of Graph
351-
* @param array $options
356+
* @param array $options - optional, an array of options
357+
* <p>Options are :<br>
358+
* <li>'excludeOrphans' - boolean value: true to exclude the orphans or false to include orphans in the result.<br>
359+
* Defaults to false</li>
360+
* <li>'_noCache' - boolean: true to not use the handler's cache for looking up prior fetched results.<br>
361+
* This will also not store the result of this call to the cache.<br>
362+
* or false to use the cache.</li>
363+
* </p>
352364
*
353365
* @return array
354366
* @throws ClientException@since 2.2
@@ -359,12 +371,33 @@ public function getVertexCollections($graph, array $options = [])
359371
$graph = $graph->getKey();
360372
}
361373

362-
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX]);
374+
$excludeOrphans = false;
375+
$_noCache = false;
376+
377+
if ((bool) $options){
378+
if (isset($options['excludeOrphans']) && !is_bool($options['excludeOrphans'])){
379+
$excludeOrphans = UrlHelper::getBoolString($options['excludeOrphans']);
380+
}
381+
382+
if (isset($options['_noCache'])){
383+
$_noCache = $options['_noCache'];
384+
}
385+
}
363386

364-
if (is_array($options) && isset($options['excludeOrphans'])) {
365-
$url = UrlHelper::appendParamsUrl($url, ['excludeOrphans' => UrlHelper::getBoolString($options['excludeOrphans'])]);
387+
if ($_noCache === false){
388+
if ($excludeOrphans===true && !empty($this->cache[$graph]['excludeOrphans']['result'])){
389+
return $this->cache[$graph]['excludeOrphans']['result'];
390+
}else if (!empty($this->cache[$graph]['result'])) {
391+
return $this->cache[$graph]['result'];
392+
}
366393
}
367394

395+
$url = UrlHelper::buildUrl(Urls::URL_GRAPH, [$graph, Urls::URLPART_VERTEX]);
396+
397+
if ($excludeOrphans===true){
398+
$url = UrlHelper::appendParamsUrl($url, ['excludeOrphans' => $excludeOrphans]);
399+
}
400+
368401
try {
369402
$response = $this->getConnection()->get($url);
370403
} catch (Exception $e) {
@@ -373,7 +406,16 @@ public function getVertexCollections($graph, array $options = [])
373406

374407
$data = $response->getJson();
375408
sort($data[self::OPTION_COLLECTIONS]);
376-
return $data[self::OPTION_COLLECTIONS];
409+
$data = $data[self::OPTION_COLLECTIONS];
410+
411+
if (!empty($this->cache[$graph]) && (!isset($options['_noCache']) || isset($options['_noCache']) && $options['_noCache'] === false)){
412+
if ($excludeOrphans===true){
413+
$this->cache[$graph]['excludeOrphans']['result'] = $data;
414+
}else{
415+
$this->cache[$graph]['result'] = $data;
416+
}
417+
}
418+
return $data;
377419
}
378420

379421
/**

0 commit comments

Comments
 (0)