#117: Avoid repeated calls to the configuration table - cache the entire table on the first request and use the cache in subsequent calls
This commit is contained in:
parent
d6d2420eb7
commit
c0ab6a7acc
@ -2,17 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Helpers;
|
namespace App\Helpers;
|
||||||
|
|
||||||
use App\Album;
|
|
||||||
use App\AlbumSources\AmazonS3Source;
|
use App\AlbumSources\AmazonS3Source;
|
||||||
use App\AlbumSources\IAlbumSource;
|
use App\AlbumSources\IAlbumSource;
|
||||||
use App\AlbumSources\LocalFilesystemSource;
|
use App\AlbumSources\LocalFilesystemSource;
|
||||||
use App\AlbumSources\OpenStackSource;
|
use App\AlbumSources\OpenStackSource;
|
||||||
use App\AlbumSources\OpenStackV1Source;
|
|
||||||
use App\AlbumSources\RackspaceSource;
|
use App\AlbumSources\RackspaceSource;
|
||||||
use App\Configuration;
|
use App\Configuration;
|
||||||
|
|
||||||
class ConfigHelper
|
class ConfigHelper
|
||||||
{
|
{
|
||||||
|
/** @var mixed Cache of configuration values */
|
||||||
|
private $cache;
|
||||||
|
|
||||||
public function allowedAlbumViews()
|
public function allowedAlbumViews()
|
||||||
{
|
{
|
||||||
return ['default', 'slideshow'];
|
return ['default', 'slideshow'];
|
||||||
@ -143,7 +144,12 @@ class ConfigHelper
|
|||||||
|
|
||||||
public function get($key, $defaultIfUnset = true)
|
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))
|
if (is_null($config))
|
||||||
{
|
{
|
||||||
@ -163,6 +169,7 @@ class ConfigHelper
|
|||||||
{
|
{
|
||||||
$results = array();
|
$results = array();
|
||||||
|
|
||||||
|
/** @var Configuration $config */
|
||||||
foreach (Configuration::all() as $config)
|
foreach (Configuration::all() as $config)
|
||||||
{
|
{
|
||||||
$results[$config->key] = $config->value;
|
$results[$config->key] = $config->value;
|
||||||
@ -173,13 +180,15 @@ class ConfigHelper
|
|||||||
|
|
||||||
public function getOrCreateModel($key)
|
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)
|
if (is_null($config) || $config === false)
|
||||||
{
|
{
|
||||||
$config = new Configuration();
|
$config = new Configuration();
|
||||||
$config->key = $key;
|
$config->key = $key;
|
||||||
$config->value = '';
|
$config->value = '';
|
||||||
$config->save();
|
$config->save();
|
||||||
|
|
||||||
|
$this->cache = $this->getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
@ -191,4 +200,15 @@ class ConfigHelper
|
|||||||
$this->get('social_twitter_login') ||
|
$this->get('social_twitter_login') ||
|
||||||
$this->get('social_google_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user