From 1064432984ace96f9379c174b12671cd2ba8500d Mon Sep 17 00:00:00 2001 From: isUnknown Date: Mon, 26 May 2025 13:48:04 +0200 Subject: [PATCH] improve cache system --- public/site/blueprints/pages/project.yml | 2 +- .../hooks/regenerate-project-steps-cache.php | 10 +++-- public/site/models/project.php | 43 ++++++++++--------- public/site/plugins/helpers/index.php | 6 +++ public/site/templates/project.json.php | 2 +- public/site/templates/projects.json.php | 2 +- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/public/site/blueprints/pages/project.yml b/public/site/blueprints/pages/project.yml index c8922a4..933e873 100644 --- a/public/site/blueprints/pages/project.yml +++ b/public/site/blueprints/pages/project.yml @@ -29,7 +29,7 @@ tabs: columns: 6 options: type: query - query: page.getSteps + query: page.getRefreshedSteps text: "{{ item.label }}" value: "{{ item.id }}" required: true diff --git a/public/site/config/hooks/regenerate-project-steps-cache.php b/public/site/config/hooks/regenerate-project-steps-cache.php index f22cd96..03c2705 100644 --- a/public/site/config/hooks/regenerate-project-steps-cache.php +++ b/public/site/config/hooks/regenerate-project-steps-cache.php @@ -1,7 +1,11 @@ template() == 'project' || $newPage->parents()->findBy('template', 'project')) { - $newPage->getSteps(true); - } + // $project = $newPage->template() == 'project' ? $newPage : $newPage->parents()->findBy('template', 'project'); + // if ($project) { + // $steps = $newPage->getRefreshedSteps(true); + + // refreshProjectStepsCache($project, $steps); + // } }; \ No newline at end of file diff --git a/public/site/models/project.php b/public/site/models/project.php index 484261d..ea6ca53 100644 --- a/public/site/models/project.php +++ b/public/site/models/project.php @@ -3,29 +3,32 @@ use adrienpayet\notifications\NotificationsPage; class ProjectPage extends NotificationsPage { - public function getSteps($refreshCache = false) { + public function getStepsFromCache() { $apiCache = kirby()->cache('api'); - $stepsData = $apiCache->get($this->slug() . '_' . 'steps'); - - if ($refreshCache) { - $steps = []; - - foreach ($this->children() as $child) { - try { - $steps[] = $this->createStep($child); - } catch (\Throwable $th) { - //throw $th; - } + $stepsData = $apiCache->get($this->slug() . '_' . 'steps'); + if (!$stepsData) { + $steps = $newPage->getRefreshedSteps(true); + + refreshProjectStepsCache($project, $steps); + return $steps; + }; + return $stepsData; + } + public function getRefreshedSteps() { + // Create steps + $steps = []; + foreach ($this->children() as $child) { + try { + $steps[] = $this->createStep($child); + } catch (\Throwable $th) { + throw new Exception("Can't create step. File " . $th->getFile() . " line " . $th->getLine(), 1); } - - usort($steps, fn($a, $b) => $a['index'] <=> $b['index']); - - $apiCache->set($this->slug() . '_' . 'steps', $steps); - - return $steps; - } else { - return $stepsData; } + + // Sort steps by their index + usort($steps, fn($a, $b) => $a['index'] <=> $b['index']); + + return $steps; } private function createStep($child) { diff --git a/public/site/plugins/helpers/index.php b/public/site/plugins/helpers/index.php index 8c916ee..1ede799 100644 --- a/public/site/plugins/helpers/index.php +++ b/public/site/plugins/helpers/index.php @@ -212,4 +212,10 @@ function processDTLProposals($page) { } return $proposals; +} + +function refreshProjectStepsCache($project, $steps) { + $apiCache = kirby()->cache('api'); + $apiCache->set($project->slug() . '_' . 'steps', $steps); + $apiCache = kirby()->cache('api'); } \ No newline at end of file diff --git a/public/site/templates/project.json.php b/public/site/templates/project.json.php index a099586..32996fc 100644 --- a/public/site/templates/project.json.php +++ b/public/site/templates/project.json.php @@ -8,7 +8,7 @@ $project = [ 'logo' => $page->client()->isNotEmpty() && $page->client()->toPage() ? $page->client()->toPage()->logo()->toFile()->url() : null, - 'steps' => $page->getSteps(), + 'steps' => $page->getStepsFromCache(), 'designToLight' => $page->isDTLEnabled()->isTrue() ? processDTLProposals($page) : null, 'hasOptimizationRequest' => $page->hasOptimizationRequest()->isTrue(), 'notifications' => $page->notifications()->yaml(), diff --git a/public/site/templates/projects.json.php b/public/site/templates/projects.json.php index cabb5c7..e5f4974 100644 --- a/public/site/templates/projects.json.php +++ b/public/site/templates/projects.json.php @@ -10,7 +10,7 @@ function getProjectData($project) 'currentStep' => $project->currentStep()->value(), 'status' => $project->status(), 'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '', - 'steps' => $project->getSteps(), + 'steps' => $project->getStepsFromCache(), 'notifications' => Yaml::decode($project->notifications()->value), 'uuid' => (string) $project->uuid(), 'slug' => (string) $project->slug(),