mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-08 18:10:08 +00:00
Merge pull request #4062 from BookStackApp/settings_perf
Changed the way settings are loaded
This commit is contained in:
commit
2724b2867b
2 changed files with 90 additions and 61 deletions
|
@ -3,45 +3,29 @@
|
||||||
namespace BookStack\Settings;
|
namespace BookStack\Settings;
|
||||||
|
|
||||||
use BookStack\Auth\User;
|
use BookStack\Auth\User;
|
||||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SettingService
|
* Class SettingService
|
||||||
* The settings are a simple key-value database store.
|
* The settings are a simple key-value database store.
|
||||||
* For non-authenticated users, user settings are stored via the session instead.
|
* For non-authenticated users, user settings are stored via the session instead.
|
||||||
|
* A local array-based cache is used to for setting accesses across a request.
|
||||||
*/
|
*/
|
||||||
class SettingService
|
class SettingService
|
||||||
{
|
{
|
||||||
protected Setting $setting;
|
|
||||||
protected Cache $cache;
|
|
||||||
protected array $localCache = [];
|
protected array $localCache = [];
|
||||||
protected string $cachePrefix = 'setting-';
|
|
||||||
|
|
||||||
public function __construct(Setting $setting, Cache $cache)
|
|
||||||
{
|
|
||||||
$this->setting = $setting;
|
|
||||||
$this->cache = $cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a setting from the database,
|
* Gets a setting from the database,
|
||||||
* If not found, Returns default, Which is false by default.
|
* If not found, Returns default, Which is false by default.
|
||||||
*/
|
*/
|
||||||
public function get(string $key, $default = null)
|
public function get(string $key, $default = null): mixed
|
||||||
{
|
{
|
||||||
if (is_null($default)) {
|
if (is_null($default)) {
|
||||||
$default = config('setting-defaults.' . $key, 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;
|
||||||
$formatted = $this->formatValue($value, $default);
|
return $this->formatValue($value, $default);
|
||||||
$this->localCache[$key] = $formatted;
|
|
||||||
|
|
||||||
return $formatted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,52 +63,78 @@ class SettingService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a setting value from the cache or database.
|
* Gets a setting value from the local cache.
|
||||||
* Looks at the system defaults if not cached or in database.
|
* Will load the local cache if not previously loaded.
|
||||||
* Returns null if nothing is found.
|
|
||||||
*/
|
*/
|
||||||
protected function getValueFromStore(string $key)
|
protected function getValueFromStore(string $key): mixed
|
||||||
{
|
{
|
||||||
// Check the cache
|
$cacheCategory = $this->localCacheCategory($key);
|
||||||
$cacheKey = $this->cachePrefix . $key;
|
if (!isset($this->localCache[$cacheCategory])) {
|
||||||
$cacheVal = $this->cache->get($cacheKey, null);
|
$this->loadToLocalCache($cacheCategory);
|
||||||
if ($cacheVal !== null) {
|
|
||||||
return $cacheVal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the database
|
return $this->localCache[$cacheCategory][$key] ?? null;
|
||||||
$settingObject = $this->getSettingObjectByKey($key);
|
|
||||||
if ($settingObject !== null) {
|
|
||||||
$value = $settingObject->value;
|
|
||||||
|
|
||||||
if ($settingObject->type === 'array') {
|
|
||||||
$value = json_decode($value, true) ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cache->forever($cacheKey, $value);
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear an item from the cache completely.
|
* Put the given value into the local cached under the given key.
|
||||||
*/
|
*/
|
||||||
protected function clearFromCache(string $key)
|
protected function putValueIntoLocalCache(string $key, mixed $value): void
|
||||||
{
|
{
|
||||||
$cacheKey = $this->cachePrefix . $key;
|
$cacheCategory = $this->localCacheCategory($key);
|
||||||
$this->cache->forget($cacheKey);
|
if (!isset($this->localCache[$cacheCategory])) {
|
||||||
if (isset($this->localCache[$key])) {
|
$this->loadToLocalCache($cacheCategory);
|
||||||
unset($this->localCache[$key]);
|
}
|
||||||
|
|
||||||
|
$this->localCache[$cacheCategory][$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the category for the given setting key.
|
||||||
|
* Will return 'app' for a general app setting otherwise 'user:<user_id>' for a user setting.
|
||||||
|
*/
|
||||||
|
protected function localCacheCategory(string $key): string
|
||||||
|
{
|
||||||
|
if (str_starts_with($key, 'user:')) {
|
||||||
|
return implode(':', array_slice(explode(':', $key), 0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'app';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For the given category, load the relevant settings from the database into the local cache.
|
||||||
|
*/
|
||||||
|
protected function loadToLocalCache(string $cacheCategory): void
|
||||||
|
{
|
||||||
|
$query = Setting::query();
|
||||||
|
|
||||||
|
if ($cacheCategory === 'app') {
|
||||||
|
$query->where('setting_key', 'not like', 'user:%');
|
||||||
|
} else {
|
||||||
|
$query->where('setting_key', 'like', $cacheCategory . ':%');
|
||||||
|
}
|
||||||
|
$settings = $query->toBase()->get();
|
||||||
|
|
||||||
|
if (!isset($this->localCache[$cacheCategory])) {
|
||||||
|
$this->localCache[$cacheCategory] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($settings as $setting) {
|
||||||
|
$value = $setting->value;
|
||||||
|
|
||||||
|
if ($setting->type === 'array') {
|
||||||
|
$value = json_decode($value, true) ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->localCache[$cacheCategory][$setting->setting_key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format a settings value.
|
* Format a settings value.
|
||||||
*/
|
*/
|
||||||
protected function formatValue($value, $default)
|
protected function formatValue(mixed $value, mixed $default): mixed
|
||||||
{
|
{
|
||||||
// Change string booleans to actual booleans
|
// Change string booleans to actual booleans
|
||||||
if ($value === 'true') {
|
if ($value === 'true') {
|
||||||
|
@ -155,21 +165,22 @@ class SettingService
|
||||||
* Add a setting to the database.
|
* Add a setting to the database.
|
||||||
* Values can be an array or a string.
|
* Values can be an array or a string.
|
||||||
*/
|
*/
|
||||||
public function put(string $key, $value): bool
|
public function put(string $key, mixed $value): bool
|
||||||
{
|
{
|
||||||
$setting = $this->setting->newQuery()->firstOrNew([
|
$setting = Setting::query()->firstOrNew([
|
||||||
'setting_key' => $key,
|
'setting_key' => $key,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$setting->type = 'string';
|
$setting->type = 'string';
|
||||||
|
$setting->value = $value;
|
||||||
|
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$setting->type = 'array';
|
$setting->type = 'array';
|
||||||
$value = $this->formatArrayValue($value);
|
$setting->value = $this->formatArrayValue($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
$setting->value = $value;
|
|
||||||
$setting->save();
|
$setting->save();
|
||||||
$this->clearFromCache($key);
|
$this->putValueIntoLocalCache($key, $value);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -209,7 +220,7 @@ class SettingService
|
||||||
* Can only take string value types since this may use
|
* Can only take string value types since this may use
|
||||||
* the session which is less flexible to data types.
|
* the session which is less flexible to data types.
|
||||||
*/
|
*/
|
||||||
public function putForCurrentUser(string $key, string $value)
|
public function putForCurrentUser(string $key, string $value): bool
|
||||||
{
|
{
|
||||||
return $this->putUser(user(), $key, $value);
|
return $this->putUser(user(), $key, $value);
|
||||||
}
|
}
|
||||||
|
@ -231,15 +242,19 @@ class SettingService
|
||||||
if ($setting) {
|
if ($setting) {
|
||||||
$setting->delete();
|
$setting->delete();
|
||||||
}
|
}
|
||||||
$this->clearFromCache($key);
|
|
||||||
|
$cacheCategory = $this->localCacheCategory($key);
|
||||||
|
if (isset($this->localCache[$cacheCategory])) {
|
||||||
|
unset($this->localCache[$cacheCategory][$key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete settings for a given user id.
|
* Delete settings for a given user id.
|
||||||
*/
|
*/
|
||||||
public function deleteUserSettings(string $userId)
|
public function deleteUserSettings(string $userId): void
|
||||||
{
|
{
|
||||||
return $this->setting->newQuery()
|
Setting::query()
|
||||||
->where('setting_key', 'like', $this->userKey($userId) . '%')
|
->where('setting_key', 'like', $this->userKey($userId) . '%')
|
||||||
->delete();
|
->delete();
|
||||||
}
|
}
|
||||||
|
@ -249,7 +264,16 @@ class SettingService
|
||||||
*/
|
*/
|
||||||
protected function getSettingObjectByKey(string $key): ?Setting
|
protected function getSettingObjectByKey(string $key): ?Setting
|
||||||
{
|
{
|
||||||
return $this->setting->newQuery()
|
return Setting::query()
|
||||||
->where('setting_key', '=', $key)->first();
|
->where('setting_key', '=', $key)
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty the local setting value cache used by this service.
|
||||||
|
*/
|
||||||
|
public function flushCache(): void
|
||||||
|
{
|
||||||
|
$this->localCache = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,8 @@ class UpdateUrlCommandTest extends TestCase
|
||||||
setting()->put('my-custom-item', 'https://example.com/donkey/cat');
|
setting()->put('my-custom-item', 'https://example.com/donkey/cat');
|
||||||
$this->runUpdate('https://example.com', 'https://cats.example.com');
|
$this->runUpdate('https://example.com', 'https://cats.example.com');
|
||||||
|
|
||||||
|
setting()->flushCache();
|
||||||
|
|
||||||
$settingVal = setting('my-custom-item');
|
$settingVal = setting('my-custom-item');
|
||||||
$this->assertEquals('https://cats.example.com/donkey/cat', $settingVal);
|
$this->assertEquals('https://cats.example.com/donkey/cat', $settingVal);
|
||||||
}
|
}
|
||||||
|
@ -47,6 +49,9 @@ class UpdateUrlCommandTest extends TestCase
|
||||||
{
|
{
|
||||||
setting()->put('my-custom-array-item', [['name' => 'a https://example.com/donkey/cat url']]);
|
setting()->put('my-custom-array-item', [['name' => 'a https://example.com/donkey/cat url']]);
|
||||||
$this->runUpdate('https://example.com', 'https://cats.example.com');
|
$this->runUpdate('https://example.com', 'https://cats.example.com');
|
||||||
|
|
||||||
|
setting()->flushCache();
|
||||||
|
|
||||||
$settingVal = setting('my-custom-array-item');
|
$settingVal = setting('my-custom-array-item');
|
||||||
$this->assertEquals('a https://cats.example.com/donkey/cat url', $settingVal[0]['name']);
|
$this->assertEquals('a https://cats.example.com/donkey/cat url', $settingVal[0]['name']);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue