Skip to content

Commit 493bbef

Browse files
implement global cache time to live. close #552
- set default to one week - move cache setting to a new admin panel
1 parent 9a2816a commit 493bbef

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

app/class/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ abstract class Config
7171
/** Indicate if img should have loading="lazy" attribute */
7272
protected static bool $lazyloadimg = true;
7373

74+
/** Global cache duration in seconds. Default is one week */
75+
protected static int $cachettl = 604800;
76+
7477
/** LDAP auth */
7578
protected static string $ldapserver = '';
7679
protected static string $ldaptree = '';
@@ -430,6 +433,11 @@ public static function lazyloadimg(): bool
430433
return self::$lazyloadimg;
431434
}
432435

436+
public static function cachettl(): int
437+
{
438+
return self::$cachettl;
439+
}
440+
433441
public static function ldapserver(): string
434442
{
435443
return self::$ldapserver;
@@ -731,6 +739,13 @@ public static function setlazyloadimg(bool $lazyloadimg): bool
731739
return self::$lazyloadimg = $lazyloadimg;
732740
}
733741

742+
public static function setcachettl(int $cachettl): void
743+
{
744+
if ($cachettl >= 0 && $cachettl <= Model::MAX_CACHE_TTL) {
745+
self::$cachettl = $cachettl;
746+
}
747+
}
748+
734749
public static function setldapserver(string $ldapserver): void
735750
{
736751
self::$ldapserver = $ldapserver;

app/class/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ abstract class Model
7272
public const PASSWORD_MAX_LENGTH = 128;
7373
public const MAX_COOKIE_CONSERVATION = 365;
7474
public const MAX_QUERY_LENGH = 512;
75+
public const MAX_CACHE_TTL = 1000000000;
7576

7677

7778
public static function dirtopath(string $dir): string

app/class/Modelpage.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,15 @@ protected function reset(Page $page, array $reset): Page
439439
* 1. render file(s) are missing
440440
* 2. edit date is more recent than render date
441441
* 3. if the templatebody is set, exist and has been updated
442+
* 4. cache time to live is reached
442443
*
443444
* @param Page $page Page to be checked
444445
*
445446
* @param int<1, max> $level
446447
*
447448
* @return bool true if the page need to be rendered otherwise false
448449
*/
449-
public function needtoberendered(Page $page, int $level = 3): bool
450+
public function needtoberendered(Page $page, int $level = 4): bool
450451
{
451452
if ($level < 1) {
452453
throw new DomainException('minimum level is 1');
@@ -462,14 +463,25 @@ public function needtoberendered(Page $page, int $level = 3): bool
462463
if ($level >= 2 && $page->daterender() <= $page->datemodif()) {
463464
return true;
464465
}
466+
465467
if ($level >= 3 && !empty($page->templatebody())) {
466468
try {
467469
$bodytemplate = $this->get($page->templatebody());
468-
return $page->daterender() <= $bodytemplate->datemodif();
470+
if ($page->daterender() <= $bodytemplate->datemodif()) {
471+
return true;
472+
}
469473
} catch (RuntimeException $e) {
470474
Logger::errorex($e);
471475
}
472476
}
477+
478+
if ($level >= 4 && Config::cachettl() !== 0) {
479+
$maxttl = $page->daterender()->getTimestamp() + Config::cachettl();
480+
if (time() > $maxttl) {
481+
return true;
482+
}
483+
}
484+
473485
return false;
474486
}
475487

app/view/templates/admin.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,6 @@
345345
<p class="info">To be applied, these modifications need the re-rendering of all pages.
346346
</p>
347347

348-
<h3>Rendering details</h3>
349-
<p class="info">When a page is modified, this may affect the rendering of other pages linked to it.
350-
The pages to which it points have a strong possibility of needing to be updated too.
351-
This option will invalidate their rendering each time the page pointing to them is updated.
352-
They will therefore be re-rendered the next time they are viewed.
353-
</p>
354-
355-
<p class="field">
356-
<input type="hidden" name="deletelinktocache" value="0" form="admin">
357-
<label for="deletelinktocache">invalidates the rendering of linked pages when updating</label>
358-
<input type="checkbox" name="deletelinktocache" id="deletelinktocache" value="1" <?= Wcms\Config::deletelinktocache() ? 'checked' : '' ?> form="admin">
359-
</p>
360-
361348
<h3>Base page language</h3>
362349

363350
<p class="info">If the page language is not specified in metadatas, then this default will be used.
@@ -448,6 +435,42 @@
448435
</p>
449436
</div>
450437

438+
<div class="grid-item" id="cache">
439+
<h2>Cache</h2>
440+
441+
<p class="info">
442+
Number of seconds before a page render expires.
443+
Set it to zero if you want no cache expiration.
444+
<br>
445+
(604800 seconds is one week, 86400 is a day, 3600 is one hour)
446+
</p>
447+
448+
<p class="field">
449+
<label for="cachettl">cache life time (in seconds)</label>
450+
<input
451+
type="number"
452+
name="cachettl"
453+
id="cachettl"
454+
value="<?= Wcms\Config::cachettl() ?>"
455+
min="0"
456+
max="<?= Wcms\Model::MAX_CACHE_TTL ?>"
457+
form="admin"
458+
>
459+
</p>
460+
461+
<p class="info">When a page is modified, this may affect the rendering of other pages linked to it.
462+
The pages to which it points have a strong possibility of needing to be updated too.
463+
This option will invalidate their rendering each time the page pointing to them is updated.
464+
They will therefore be re-rendered the next time they are viewed.
465+
</p>
466+
467+
<p class="field">
468+
<input type="hidden" name="deletelinktocache" value="0" form="admin">
469+
<label for="deletelinktocache">invalidates the rendering of linked pages when updating</label>
470+
<input type="checkbox" name="deletelinktocache" id="deletelinktocache" value="1" <?= Wcms\Config::deletelinktocache() ? 'checked' : '' ?> form="admin">
471+
</p>
472+
</div>
473+
451474
<div class="grid-item" id="style">
452475

453476
<h2>Style</h2>

0 commit comments

Comments
 (0)