From 5597ce58d2b35d7bcc1caf8195778103a111d664 Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Fri, 11 Dec 2015 23:02:32 +0100 Subject: [PATCH] baculum: Add session cache --- gui/baculum/protected/Class/API.php | 63 ++++++++++++++++--- gui/baculum/protected/Pages/Home.php | 5 ++ .../protected/Portlets/JobConfiguration.php | 18 +++--- .../Portlets/JobRunConfiguration.php | 10 +-- 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/gui/baculum/protected/Class/API.php b/gui/baculum/protected/Class/API.php index ad7a0d7bc0..d4071a07d2 100644 --- a/gui/baculum/protected/Class/API.php +++ b/gui/baculum/protected/Class/API.php @@ -49,6 +49,7 @@ class API extends TModule { } public function init($config) { + $this->initSessionCache(); $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig(); } @@ -77,15 +78,28 @@ class API extends TModule { * API REQUESTS METHODS (get, set, create, delete) */ - public function get(array $params) { - $url = $this->getURL() . implode('/', $params); - $this->setParamsToUrl($url); - $ch = $this->getConnection(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json')); - $result = curl_exec($ch); - curl_close($ch); - return $this->preParseOutput($result); + public function get(array $params, $use_cache = false) { + $cached = null; + $ret = null; + if ($use_cache === true) { + $cached = $this->getSessionCache($params); + } + if (!is_null($cached)) { + $ret = $cached; + } else { + $url = $this->getURL() . implode('/', $params); + $this->setParamsToUrl($url); + $ch = $this->getConnection(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->getAPIHeader(), 'Accept: application/json')); + $result = curl_exec($ch); + curl_close($ch); + $ret = $this->preParseOutput($result); + if ($use_cache === true && $ret->error === 0) { + $this->setSessionCache($params, $ret); + } + } + return $ret; } public function set(array $params, array $options) { @@ -149,5 +163,36 @@ class API extends TModule { return $resource; } + + public function initSessionCache($force = false) { + if (!isset($_SESSION) || !array_key_exists('cache', $_SESSION) || !is_array($_SESSION['cache']) || $force === true) { + $_SESSION['cache'] = array(); + } + } + + private function getSessionCache(array $params) { + $cached = null; + $key = $this->getSessionKey($params); + if ($this->isSessionValue($key)) { + $cached = $_SESSION['cache'][$key]; + } + return $cached; + } + + private function setSessionCache(array $params, $value) { + $key = $this->getSessionKey($params); + $_SESSION['cache'][$key] = $value; + } + + private function getSessionKey(array $params) { + $key = implode(';', $params); + $key = base64_encode($key); + return $key; + } + + private function isSessionValue($key) { + $is_value = array_key_exists($key, $_SESSION['cache']); + return $is_value; + } } ?> diff --git a/gui/baculum/protected/Pages/Home.php b/gui/baculum/protected/Pages/Home.php index 38c4d6a64b..9cf0aaeac2 100644 --- a/gui/baculum/protected/Pages/Home.php +++ b/gui/baculum/protected/Pages/Home.php @@ -36,6 +36,11 @@ class Home extends BaculumPage public function onInit($param) { parent::onInit($param); + + if (!$this->IsPostBack && !$this->IsCallBack) { + $this->getModule('api')->initSessionCache(true); + } + $isConfigExists = $this->getModule('configuration')->isApplicationConfig(); if($isConfigExists === false) { $this->goToPage('ConfigurationWizard'); diff --git a/gui/baculum/protected/Portlets/JobConfiguration.php b/gui/baculum/protected/Portlets/JobConfiguration.php index 35c3c0fd09..b32b86681a 100644 --- a/gui/baculum/protected/Portlets/JobConfiguration.php +++ b/gui/baculum/protected/Portlets/JobConfiguration.php @@ -61,7 +61,7 @@ class JobConfiguration extends Portlets { $this->JobToVerifyOptions->dataSource = $verifyValues; $this->JobToVerifyOptions->dataBind(); - $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'))->output; + $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output; $jobsAllDirs = array(); foreach($jobTasks as $director => $tasks) { @@ -71,7 +71,7 @@ class JobConfiguration extends Portlets { $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs); $this->JobToVerifyJobName->dataBind(); - $clients = $this->Application->getModule('api')->get(array('clients'))->output; + $clients = $this->Application->getModule('api')->get(array('clients'), true)->output; $clientsList = array(); foreach($clients as $client) { $clientsList[$client->clientid] = $client->name; @@ -80,19 +80,19 @@ class JobConfiguration extends Portlets { $this->Client->SelectedValue = $jobdata->clientid; $this->Client->dataBind(); - $filesetsAll = $this->Application->getModule('api')->get(array('filesets'))->output; + $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), true)->output; $filesetsList = array(); foreach($filesetsAll as $director => $filesets) { $filesetsList = array_merge($filesets, $filesetsList); } if($jobdata->filesetid != 0) { - $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid))->output; + $selectedFileset = $this->Application->getModule('api')->get(array('filesets', $jobdata->filesetid), true)->output; } $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList); $this->FileSet->SelectedValue = @$selectedFileset->fileset; $this->FileSet->dataBind(); - $pools = $this->Application->getModule('api')->get(array('pools'))->output; + $pools = $this->Application->getModule('api')->get(array('pools'), true)->output; $poolList = array(); foreach($pools as $pool) { $poolList[$pool->poolid] = $pool->name; @@ -101,11 +101,11 @@ class JobConfiguration extends Portlets { $this->Pool->SelectedValue = $jobdata->poolid; $this->Pool->dataBind(); - $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid))->output; + $jobshow = $this->Application->getModule('api')->get(array('jobs', 'show', $jobdata->jobid), true)->output; $storageShow = $this->getResourceName('storage', $jobshow); $storagesList = array(); $selectedStorageId = null; - $storages = $this->Application->getModule('api')->get(array('storages'))->output; + $storages = $this->Application->getModule('api')->get(array('storages'), true)->output; foreach($storages as $storage) { if ($storage->name == $storageShow) { $selectedStorageId = $storage->storageid; @@ -244,7 +244,7 @@ class JobConfiguration extends Portlets { private function getPoolByName($name) { $pool = null; - $pools = $this->Application->getModule('api')->get(array('pools'))->output; + $pools = $this->Application->getModule('api')->get(array('pools'), true)->output; for ($i = 0; $i < count($pools); $i++) { if ($pools[$i]->name == $name) { $pool = $pools[$i]; @@ -256,7 +256,7 @@ class JobConfiguration extends Portlets { private function getStorageByName($name) { $storage = null; - $storages = $this->Application->getModule('api')->get(array('storages'))->output; + $storages = $this->Application->getModule('api')->get(array('storages'), true)->output; for ($i = 0; $i < count($storages); $i++) { if ($storages[$i]->name == $name) { $storage = $storages[$i]; diff --git a/gui/baculum/protected/Portlets/JobRunConfiguration.php b/gui/baculum/protected/Portlets/JobRunConfiguration.php index 90c543cc65..f3ea6a0006 100644 --- a/gui/baculum/protected/Portlets/JobRunConfiguration.php +++ b/gui/baculum/protected/Portlets/JobRunConfiguration.php @@ -53,7 +53,7 @@ class JobRunConfiguration extends Portlets { $this->JobToVerifyOptions->dataSource = $verifyValues; $this->JobToVerifyOptions->dataBind(); - $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'))->output; + $jobTasks = $this->Application->getModule('api')->get(array('jobs', 'tasks'), true)->output; $jobsAllDirs = array(); foreach($jobTasks as $director => $tasks) { @@ -63,7 +63,7 @@ class JobRunConfiguration extends Portlets { $this->JobToVerifyJobName->dataSource = array_combine($jobsAllDirs, $jobsAllDirs); $this->JobToVerifyJobName->dataBind(); - $clients = $this->Application->getModule('api')->get(array('clients'))->output; + $clients = $this->Application->getModule('api')->get(array('clients'), true)->output; $clientsList = array(); foreach($clients as $client) { $clientsList[$client->clientid] = $client->name; @@ -71,7 +71,7 @@ class JobRunConfiguration extends Portlets { $this->Client->dataSource = $clientsList; $this->Client->dataBind(); - $filesetsAll = $this->Application->getModule('api')->get(array('filesets'))->output; + $filesetsAll = $this->Application->getModule('api')->get(array('filesets'), true)->output; $filesetsList = array(); foreach($filesetsAll as $director => $filesets) { $filesetsList = array_merge($filesets, $filesetsList); @@ -79,7 +79,7 @@ class JobRunConfiguration extends Portlets { $this->FileSet->dataSource = array_combine($filesetsList, $filesetsList); $this->FileSet->dataBind(); - $pools = $this->Application->getModule('api')->get(array('pools'))->output; + $pools = $this->Application->getModule('api')->get(array('pools'), true)->output; $poolList = array(); foreach($pools as $pool) { $poolList[$pool->poolid] = $pool->name; @@ -87,7 +87,7 @@ class JobRunConfiguration extends Portlets { $this->Pool->dataSource = $poolList; $this->Pool->dataBind(); - $storages = $this->Application->getModule('api')->get(array('storages'))->output; + $storages = $this->Application->getModule('api')->get(array('storages'), true)->output; $storagesList = array(); foreach($storages as $storage) { $storagesList[$storage->storageid] = $storage->name; -- 2.39.5