improve cache system

This commit is contained in:
isUnknown 2025-05-26 13:48:04 +02:00
parent ca0ca3b2a2
commit 1064432984
6 changed files with 39 additions and 26 deletions

View file

@ -29,7 +29,7 @@ tabs:
columns: 6
options:
type: query
query: page.getSteps
query: page.getRefreshedSteps
text: "{{ item.label }}"
value: "{{ item.id }}"
required: true

View file

@ -1,7 +1,11 @@
<?php
// page.update:after
return function($newPage, $oldPage) {
if ($newPage->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);
// }
};

View file

@ -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) {

View file

@ -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');
}

View file

@ -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(),

View file

@ -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(),