From 40538c51cecf90bc9a237cc3d56f1f2c42f73f91 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 25 Feb 2019 09:41:23 +0100 Subject: [PATCH] LegacyStorage: Add cache to avoid parsing configurations multiple times --- application/clicommands/ProcessCommand.php | 2 +- configuration.php | 4 +--- library/Businessprocess/BpConfig.php | 4 +--- .../Director/ShipConfigFiles.php | 5 +--- .../Businessprocess/Storage/LegacyStorage.php | 23 +++++++++++++++---- library/Businessprocess/Storage/Storage.php | 15 ++++++++++++ library/Businessprocess/Web/Controller.php | 4 +--- 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/application/clicommands/ProcessCommand.php b/application/clicommands/ProcessCommand.php index 5280e37..a11a202 100644 --- a/application/clicommands/ProcessCommand.php +++ b/application/clicommands/ProcessCommand.php @@ -34,7 +34,7 @@ class ProcessCommand extends Command public function init() { - $this->storage = new LegacyStorage($this->Config()->getSection('global')); + $this->storage = LegacyStorage::getInstance(); } /** diff --git a/configuration.php b/configuration.php index 38ef46a..cf5c86a 100644 --- a/configuration.php +++ b/configuration.php @@ -10,9 +10,7 @@ $section = $this->menuSection(N_('Business Processes'), array( )); try { - $storage = new LegacyStorage( - $this->getConfig()->getSection('global') - ); + $storage = LegacyStorage::getInstance(); $prio = 0; foreach ($storage->listProcessNames() as $name) { diff --git a/library/Businessprocess/BpConfig.php b/library/Businessprocess/BpConfig.php index 861505a..93d76c3 100644 --- a/library/Businessprocess/BpConfig.php +++ b/library/Businessprocess/BpConfig.php @@ -631,9 +631,7 @@ class BpConfig protected function storage() { if ($this->storage === null) { - $this->storage = new LegacyStorage( - Config::module('businessprocess')->getSection('global') - ); + $this->storage = LegacyStorage::getInstance(); } return $this->storage; diff --git a/library/Businessprocess/Director/ShipConfigFiles.php b/library/Businessprocess/Director/ShipConfigFiles.php index 35019d9..17b9e1f 100644 --- a/library/Businessprocess/Director/ShipConfigFiles.php +++ b/library/Businessprocess/Director/ShipConfigFiles.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Businessprocess\Director; -use Icinga\Application\Config; use Icinga\Module\Director\Hook\ShipConfigFilesHook; use Icinga\Module\Businessprocess\Storage\LegacyStorage; @@ -12,9 +11,7 @@ class ShipConfigFiles extends ShipConfigFilesHook { $files = array(); - $storage = new LegacyStorage( - Config::module('businessprocess')->getSection('global') - ); + $storage = LegacyStorage::getInstance(); foreach ($storage->listProcesses() as $name => $title) { $files['processes/' . $name . '.bp'] = $storage->getSource($name); diff --git a/library/Businessprocess/Storage/LegacyStorage.php b/library/Businessprocess/Storage/LegacyStorage.php index 8cbe89b..8d042b0 100644 --- a/library/Businessprocess/Storage/LegacyStorage.php +++ b/library/Businessprocess/Storage/LegacyStorage.php @@ -9,6 +9,13 @@ use Icinga\Exception\SystemPermissionException; class LegacyStorage extends Storage { + /** + * All parsed configurations + * + * @var BpConfig[] + */ + protected $configs = []; + /** @var string */ protected $configDir; @@ -116,10 +123,14 @@ class LegacyStorage extends Storage */ public function loadProcess($name) { - return LegacyConfigParser::parseFile( - $name, - $this->getFilename($name) - ); + if (! isset($this->configs[$name])) { + $this->configs[$name] = LegacyConfigParser::parseFile( + $name, + $this->getFilename($name) + ); + } + + return $this->configs[$name]; } /** @@ -146,6 +157,10 @@ class LegacyStorage extends Storage */ public function loadMetadata($name) { + if (isset($this->configs[$name])) { + return $this->configs[$name]->getMetadata(); + } + return LegacyConfigParser::readMetadataFromFileHeader( $name, $this->getFilename($name) diff --git a/library/Businessprocess/Storage/Storage.php b/library/Businessprocess/Storage/Storage.php index de3d939..c8a07ba 100644 --- a/library/Businessprocess/Storage/Storage.php +++ b/library/Businessprocess/Storage/Storage.php @@ -2,12 +2,18 @@ namespace Icinga\Module\Businessprocess\Storage; +use Icinga\Application\Config; use Icinga\Data\ConfigObject; use Icinga\Module\Businessprocess\BpConfig; use Icinga\Module\Businessprocess\Metadata; abstract class Storage { + /** + * @var static + */ + protected static $instance; + /** * @var ConfigObject */ @@ -27,6 +33,15 @@ abstract class Storage { } + public static function getInstance() + { + if (static::$instance === null) { + static::$instance = new static(Config::module('businessprocess')->getSection('global')); + } + + return static::$instance; + } + /** * All processes readable by the current user * diff --git a/library/Businessprocess/Web/Controller.php b/library/Businessprocess/Web/Controller.php index b69f20e..d1104d8 100644 --- a/library/Businessprocess/Web/Controller.php +++ b/library/Businessprocess/Web/Controller.php @@ -262,9 +262,7 @@ class Controller extends ModuleController protected function storage() { if ($this->storage === null) { - $this->storage = new LegacyStorage( - $this->Config()->getSection('global') - ); + $this->storage = LegacyStorage::getInstance(); } return $this->storage;