From 65899a3e91911d7e7199eae8e33cb5f4fa6ce929 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 5 Feb 2017 18:57:57 +0000 Subject: [PATCH] Prevented settings being overfetched from db/cache --- app/Providers/AppServiceProvider.php | 6 +++++- app/Services/SettingService.php | 12 ++++++++---- app/helpers.php | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 118271f51..49cc15dd6 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -1,5 +1,7 @@ app->singleton(SettingService::class, function($app) { + return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository')); + }); } } diff --git a/app/Services/SettingService.php b/app/Services/SettingService.php index 40094a513..18a7c0d1b 100644 --- a/app/Services/SettingService.php +++ b/app/Services/SettingService.php @@ -16,6 +16,7 @@ class SettingService protected $setting; protected $cache; + protected $localCache = []; protected $cachePrefix = 'setting-'; @@ -40,8 +41,12 @@ class SettingService public function get($key, $default = false) { if ($default === false) $default = config('setting-defaults.' . $key, false); + if (isset($this->localCache[$key])) return $this->localCache[$key]; + $value = $this->getValueFromStore($key, $default); - return $this->formatValue($value, $default); + $formatted = $this->formatValue($value, $default); + $this->localCache[$key] = $formatted; + return $formatted; } /** @@ -71,9 +76,8 @@ class SettingService // Check the cache $cacheKey = $this->cachePrefix . $key; - if ($this->cache->has($cacheKey)) { - return $this->cache->get($cacheKey); - } + $cacheVal = $this->cache->get($cacheKey, null); + if ($cacheVal !== null) return $cacheVal; // Check the database $settingObject = $this->getSettingObjectByKey($key); diff --git a/app/helpers.php b/app/helpers.php index 8103ad1ff..f69f2c174 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -64,7 +64,7 @@ function userCan($permission, Ownable $ownable = null) */ function setting($key = null, $default = false) { - $settingService = app(\BookStack\Services\SettingService::class); + $settingService = resolve(\BookStack\Services\SettingService::class); if (is_null($key)) return $settingService; return $settingService->get($key, $default); }