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 columns: 6
options: options:
type: query type: query
query: page.getSteps query: page.getRefreshedSteps
text: "{{ item.label }}" text: "{{ item.label }}"
value: "{{ item.id }}" value: "{{ item.id }}"
required: true required: true

View file

@ -1,7 +1,11 @@
<?php <?php
// page.update:after
return function($newPage, $oldPage) { return function($newPage, $oldPage) {
if ($newPage->template() == 'project' || $newPage->parents()->findBy('template', 'project')) { // $project = $newPage->template() == 'project' ? $newPage : $newPage->parents()->findBy('template', 'project');
$newPage->getSteps(true); // if ($project) {
} // $steps = $newPage->getRefreshedSteps(true);
// refreshProjectStepsCache($project, $steps);
// }
}; };

View file

@ -3,29 +3,32 @@
use adrienpayet\notifications\NotificationsPage; use adrienpayet\notifications\NotificationsPage;
class ProjectPage extends NotificationsPage { class ProjectPage extends NotificationsPage {
public function getSteps($refreshCache = false) { public function getStepsFromCache() {
$apiCache = kirby()->cache('api'); $apiCache = kirby()->cache('api');
$stepsData = $apiCache->get($this->slug() . '_' . 'steps'); $stepsData = $apiCache->get($this->slug() . '_' . 'steps');
if (!$stepsData) {
$steps = $newPage->getRefreshedSteps(true);
if ($refreshCache) { refreshProjectStepsCache($project, $steps);
$steps = [];
foreach ($this->children() as $child) {
try {
$steps[] = $this->createStep($child);
} catch (\Throwable $th) {
//throw $th;
}
}
usort($steps, fn($a, $b) => $a['index'] <=> $b['index']);
$apiCache->set($this->slug() . '_' . 'steps', $steps);
return $steps; return $steps;
} else { };
return $stepsData; 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);
}
} }
// Sort steps by their index
usort($steps, fn($a, $b) => $a['index'] <=> $b['index']);
return $steps;
} }
private function createStep($child) { private function createStep($child) {

View file

@ -213,3 +213,9 @@ function processDTLProposals($page) {
return $proposals; 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() 'logo' => $page->client()->isNotEmpty() && $page->client()->toPage()
? $page->client()->toPage()->logo()->toFile()->url() ? $page->client()->toPage()->logo()->toFile()->url()
: null, : null,
'steps' => $page->getSteps(), 'steps' => $page->getStepsFromCache(),
'designToLight' => $page->isDTLEnabled()->isTrue() ? processDTLProposals($page) : null, 'designToLight' => $page->isDTLEnabled()->isTrue() ? processDTLProposals($page) : null,
'hasOptimizationRequest' => $page->hasOptimizationRequest()->isTrue(), 'hasOptimizationRequest' => $page->hasOptimizationRequest()->isTrue(),
'notifications' => $page->notifications()->yaml(), 'notifications' => $page->notifications()->yaml(),

View file

@ -10,7 +10,7 @@ function getProjectData($project)
'currentStep' => $project->currentStep()->value(), 'currentStep' => $project->currentStep()->value(),
'status' => $project->status(), 'status' => $project->status(),
'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '', 'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '',
'steps' => $project->getSteps(), 'steps' => $project->getStepsFromCache(),
'notifications' => Yaml::decode($project->notifications()->value), 'notifications' => Yaml::decode($project->notifications()->value),
'uuid' => (string) $project->uuid(), 'uuid' => (string) $project->uuid(),
'slug' => (string) $project->slug(), 'slug' => (string) $project->slug(),