From ddcc48e2ba2984d814758a4c306b754f58576a49 Mon Sep 17 00:00:00 2001 From: Fabian Bettag Date: Mon, 10 Jul 2023 11:50:48 +0200 Subject: [PATCH] Remove duplicate database calls for AppSettings --- src/Services/Settings/SettingsGroup.php | 6 ++++-- src/TwillAppSettings.php | 15 +++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Services/Settings/SettingsGroup.php b/src/Services/Settings/SettingsGroup.php index 63701a016..60d987a59 100644 --- a/src/Services/Settings/SettingsGroup.php +++ b/src/Services/Settings/SettingsGroup.php @@ -18,6 +18,8 @@ class SettingsGroup private ?Closure $availableWhen = null; + private ?AppSetting $appSetting = null; + public static function make(): self { return new self(); @@ -70,12 +72,12 @@ public function hasSection(string $sectionName): bool public function getSettingsModel(): AppSetting { - $settingsModel = AppSetting::where(['name' => $this->getName()])->first(); + $settingsModel = $this->appSetting ?? AppSetting::where(['name' => $this->getName()])->first(); if (!$settingsModel) { $settingsModel = AppSetting::create(['name' => $this->getName()]); } - return $settingsModel; + return $this->appSetting = $settingsModel; } /** diff --git a/src/TwillAppSettings.php b/src/TwillAppSettings.php index 32de3b73f..f6c3b6344 100644 --- a/src/TwillAppSettings.php +++ b/src/TwillAppSettings.php @@ -16,6 +16,8 @@ class TwillAppSettings */ private array $settingsGroups = []; + private array $groupBlockCache = []; + public function registerSettingsGroup(SettingsGroup $section): void { $this->settingsGroups[$section->getName()] = $section; @@ -103,12 +105,17 @@ public function getGroupForName(string $groupName): SettingsGroup public function getGroupDataForSectionAndName(string $group, string $section): Block { + $cacheKey = $group . $section; + if (array_key_exists($cacheKey, $this->groupBlockCache)) { + return $this->groupBlockCache[$cacheKey]; + } + $groupObject = $this->getGroupForGroupAndSectionName($group, $section); - return $groupObject->getSettingsModel()->blocks() - ->where('editor_name', $section) - ->where('parent_id', null) - ->firstOrFail(); + return $this->groupBlockCache[$cacheKey] = $groupObject->getSettingsModel()->blocks() + ->where('editor_name', $section) + ->where('parent_id', null) + ->firstOrFail(); } public function getBlockServiceForGroupAndSection(string $group, string $section): BlockService