mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 05:36:00 +00:00
Prevented settings being overfetched from db/cache
This commit is contained in:
parent
86625a7642
commit
65899a3e91
@ -1,5 +1,7 @@
|
|||||||
<?php namespace BookStack\Providers;
|
<?php namespace BookStack\Providers;
|
||||||
|
|
||||||
|
use BookStack\Services\SettingService;
|
||||||
|
use BookStack\Setting;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Validator;
|
use Validator;
|
||||||
|
|
||||||
@ -30,6 +32,8 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
//
|
$this->app->singleton(SettingService::class, function($app) {
|
||||||
|
return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ class SettingService
|
|||||||
|
|
||||||
protected $setting;
|
protected $setting;
|
||||||
protected $cache;
|
protected $cache;
|
||||||
|
protected $localCache = [];
|
||||||
|
|
||||||
protected $cachePrefix = 'setting-';
|
protected $cachePrefix = 'setting-';
|
||||||
|
|
||||||
@ -40,8 +41,12 @@ class SettingService
|
|||||||
public function get($key, $default = false)
|
public function get($key, $default = false)
|
||||||
{
|
{
|
||||||
if ($default === false) $default = config('setting-defaults.' . $key, false);
|
if ($default === false) $default = config('setting-defaults.' . $key, false);
|
||||||
|
if (isset($this->localCache[$key])) return $this->localCache[$key];
|
||||||
|
|
||||||
$value = $this->getValueFromStore($key, $default);
|
$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
|
// Check the cache
|
||||||
$cacheKey = $this->cachePrefix . $key;
|
$cacheKey = $this->cachePrefix . $key;
|
||||||
if ($this->cache->has($cacheKey)) {
|
$cacheVal = $this->cache->get($cacheKey, null);
|
||||||
return $this->cache->get($cacheKey);
|
if ($cacheVal !== null) return $cacheVal;
|
||||||
}
|
|
||||||
|
|
||||||
// Check the database
|
// Check the database
|
||||||
$settingObject = $this->getSettingObjectByKey($key);
|
$settingObject = $this->getSettingObjectByKey($key);
|
||||||
|
@ -64,7 +64,7 @@ function userCan($permission, Ownable $ownable = null)
|
|||||||
*/
|
*/
|
||||||
function setting($key = null, $default = false)
|
function setting($key = null, $default = false)
|
||||||
{
|
{
|
||||||
$settingService = app(\BookStack\Services\SettingService::class);
|
$settingService = resolve(\BookStack\Services\SettingService::class);
|
||||||
if (is_null($key)) return $settingService;
|
if (is_null($key)) return $settingService;
|
||||||
return $settingService->get($key, $default);
|
return $settingService->get($key, $default);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user