diff --git a/app/Helpers/ConfigHelper.php b/app/Helpers/ConfigHelper.php index 05424d1..f179673 100644 --- a/app/Helpers/ConfigHelper.php +++ b/app/Helpers/ConfigHelper.php @@ -2,17 +2,18 @@ namespace App\Helpers; -use App\Album; use App\AlbumSources\AmazonS3Source; use App\AlbumSources\IAlbumSource; use App\AlbumSources\LocalFilesystemSource; use App\AlbumSources\OpenStackSource; -use App\AlbumSources\OpenStackV1Source; use App\AlbumSources\RackspaceSource; use App\Configuration; class ConfigHelper { + /** @var mixed Cache of configuration values */ + private $cache; + public function allowedAlbumViews() { return ['default', 'slideshow']; @@ -143,7 +144,12 @@ class ConfigHelper public function get($key, $defaultIfUnset = true) { - $config = Configuration::where('key', $key)->first(); + if (is_null($this->cache)) + { + $this->loadCache(); + } + + $config = isset($this->cache[$key]) ? $this->cache[$key] : null; if (is_null($config)) { @@ -163,6 +169,7 @@ class ConfigHelper { $results = array(); + /** @var Configuration $config */ foreach (Configuration::all() as $config) { $results[$config->key] = $config->value; @@ -173,13 +180,15 @@ class ConfigHelper public function getOrCreateModel($key) { - $config = Configuration::where('key', $key)->first(); + $config = isset($this->cache[$key]) ? $this->cache[$key] : null; if (is_null($config) || $config === false) { $config = new Configuration(); $config->key = $key; $config->value = ''; $config->save(); + + $this->cache = $this->getAll(); } return $config; @@ -191,4 +200,15 @@ class ConfigHelper $this->get('social_twitter_login') || $this->get('social_google_login'); } + + private function loadCache() + { + $this->cache = null; + + /** @var Configuration $config */ + foreach (Configuration::all() as $config) + { + $this->cache[$config->key] = $config; + } + } } \ No newline at end of file