Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate database calls for AppSettings #2304

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Services/Settings/SettingsGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SettingsGroup

private ?Closure $availableWhen = null;

private ?AppSetting $appSetting = null;

public static function make(): self
{
return new self();
Expand Down Expand Up @@ -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;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/TwillAppSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TwillAppSettings
*/
private array $settingsGroups = [];

private array $groupBlockCache = [];

public function registerSettingsGroup(SettingsGroup $section): void
{
$this->settingsGroups[$section->getName()] = $section;
Expand Down Expand Up @@ -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
Expand Down
Loading