]> git.sur5r.net Git - bacula/bacula/commitdiff
baculum: Add session cache
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 11 Dec 2015 22:02:32 +0000 (23:02 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Fri, 11 Dec 2015 23:02:50 +0000 (00:02 +0100)
gui/baculum/protected/Class/API.php
gui/baculum/protected/Pages/Home.php
gui/baculum/protected/Portlets/JobConfiguration.php
gui/baculum/protected/Portlets/JobRunConfiguration.php

index ad7a0d7bc0751b03fd2e16532ef6423a63df8381..d4071a07d2b59a59f43271fe00eef6d86636fcf3 100644 (file)
@@ -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;
+       }
 }
 ?>
index 38c4d6a64b9da0dd3bd209f1f296506e8f5c17dd..9cf0aaeac221fc3c7d040921637d539abf61155b 100644 (file)
@@ -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');
index 35c3c0fd09104696bbb64bc5dc4f1ccea3f62b6d..b32b86681acbb1d69405959e4e9786a959f54b62 100644 (file)
@@ -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];
index 90c543cc65582bd006797e7f0584bf6103abc3ba..f3ea6a000699f03163bc9220c3a986d8f42eb867 100644 (file)
@@ -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;