Skip to content

Commit

Permalink
Don’t cache changes versions in pages cache
Browse files Browse the repository at this point in the history
Updating a changes version does not flush the pages cache (would be pretty wasteful), so rendered changes versions should also never be cached.

Extra logic is needed because one can now manually pass the changes version to render, even if no request param is set (which would also prevent caching with existing logic).
  • Loading branch information
lukasbestle committed Dec 2, 2024
1 parent 79baa7b commit e79cc36
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 50 deletions.
14 changes: 10 additions & 4 deletions src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,15 @@ public function blueprints(string|null $inSection = null): array
/**
* Builds the cache id for the page
*/
protected function cacheId(string $contentType): string
protected function cacheId(string $contentType, VersionId $versionId): string
{
$cacheId = [$this->id()];

if ($this->kirby()->multilang() === true) {
$cacheId[] = $this->kirby()->language()->code();
}

$cacheId[] = $versionId->value();
$cacheId[] = $contentType;

return implode('.', $cacheId);
Expand Down Expand Up @@ -551,7 +552,7 @@ public function isAncestorOf(Page $child): bool
* pages cache. This will also check if one
* of the ignore rules from the config kick in.
*/
public function isCacheable(): bool
public function isCacheable(VersionId|null $versionId = null): bool
{
$kirby = $this->kirby();
$cache = $kirby->cache('pages');
Expand All @@ -563,6 +564,11 @@ public function isCacheable(): bool
return false;
}

// updating the changes version does not flush the pages cache
if ($versionId?->is('changes') === true) {
return false;
}

// inspect the current request
$request = $kirby->request();

Expand Down Expand Up @@ -962,9 +968,9 @@ public function render(
$versionId = VersionId::from($versionId);

// try to get the page from cache
if ($data === [] && $this->isCacheable() === true) {
if ($data === [] && $this->isCacheable($versionId) === true) {
$cache = $kirby->cache('pages');
$cacheId = $this->cacheId($contentType);
$cacheId = $this->cacheId($contentType, $versionId);
$result = $cache->get($cacheId);
$html = $result['html'] ?? null;
$response = $result['response'] ?? [];
Expand Down
74 changes: 28 additions & 46 deletions tests/Cms/Pages/PageRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function testIsCacheableRequestMethod($method, $expected)
]);

$this->assertSame($expected, $app->page('default')->isCacheable());
$this->assertSame($expected, $app->page('default')->isCacheable(VersionId::latest()));
$this->assertFalse($app->page('default')->isCacheable(VersionId::changes()));
}

/**
Expand Down Expand Up @@ -195,7 +197,11 @@ public function testIsCacheableIgnoreId()
]);

$this->assertTrue($app->page('default')->isCacheable());
$this->assertTrue($app->page('default')->isCacheable(VersionId::latest()));
$this->assertFalse($app->page('default')->isCacheable(VersionId::changes()));
$this->assertFalse($app->page('data')->isCacheable());
$this->assertFalse($app->page('data')->isCacheable(VersionId::latest()));
$this->assertFalse($app->page('data')->isCacheable(VersionId::changes()));
}

/**
Expand All @@ -212,7 +218,11 @@ public function testIsCacheableIgnoreCallback()
]);

$this->assertFalse($app->page('default')->isCacheable());
$this->assertFalse($app->page('default')->isCacheable(VersionId::latest()));
$this->assertFalse($app->page('default')->isCacheable(VersionId::changes()));
$this->assertTrue($app->page('data')->isCacheable());
$this->assertTrue($app->page('data')->isCacheable(VersionId::latest()));
$this->assertFalse($app->page('data')->isCacheable(VersionId::changes()));
}

/**
Expand Down Expand Up @@ -250,12 +260,12 @@ public function testRenderCache()
$cache = $this->app->cache('pages');
$page = $this->app->page('default');

$this->assertNull($cache->retrieve('default.html'));
$this->assertNull($cache->retrieve('default.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$value = $cache->retrieve('default.html');
$value = $cache->retrieve('default.latest.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($html1, $value->value()['html']);
$this->assertNull($value->expires());
Expand All @@ -273,11 +283,11 @@ public function testRenderCacheCustomExpiry()
$cache = $this->app->cache('pages');
$page = $this->app->page('expiry');

$this->assertNull($cache->retrieve('expiry.html'));
$this->assertNull($cache->retrieve('expiry.latest.html'));

$time = $page->render();

$value = $cache->retrieve('expiry.html');
$value = $cache->retrieve('expiry.latest.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($time, $value->value()['html']);
$this->assertSame((int)$time, $value->expires());
Expand All @@ -292,7 +302,7 @@ public function testRenderCacheMetadata()
$cache = $this->app->cache('pages');
$page = $this->app->page('metadata');

$this->assertNull($cache->retrieve('metadata.html'));
$this->assertNull($cache->retrieve('metadata.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);
Expand Down Expand Up @@ -323,12 +333,12 @@ public function testRenderCacheDisabled()
$cache = $this->app->cache('pages');
$page = $this->app->page('disabled');

$this->assertNull($cache->retrieve('disabled.html'));
$this->assertNull($cache->retrieve('disabled.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$this->assertNull($cache->retrieve('disabled.html'));
$this->assertNull($cache->retrieve('disabled.latest.html'));

$html2 = $page->render();
$this->assertStringStartsWith('This is a test:', $html2);
Expand All @@ -355,12 +365,12 @@ public function testRenderCacheDynamicNonActive(string $slug, array $dynamicElem
$cache = $this->app->cache('pages');
$page = $this->app->page($slug);

$this->assertNull($cache->retrieve($slug . '.html'));
$this->assertNull($cache->retrieve($slug . '.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$cacheValue = $cache->retrieve($slug . '.html');
$cacheValue = $cache->retrieve($slug . '.latest.html');
$this->assertNotNull($cacheValue);
$this->assertSame(in_array('auth', $dynamicElements), $cacheValue->value()['usesAuth']);
if (in_array('cookie', $dynamicElements)) {
Expand Down Expand Up @@ -394,12 +404,12 @@ public function testRenderCacheDynamicActiveOnFirstRender(string $slug, array $d
$cache = $this->app->cache('pages');
$page = $this->app->page($slug);

$this->assertNull($cache->retrieve($slug . '.html'));
$this->assertNull($cache->retrieve($slug . '.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$cacheValue = $cache->retrieve($slug . '.html');
$cacheValue = $cache->retrieve($slug . '.latest.html');
$this->assertNull($cacheValue);

// reset the Kirby Responder object
Expand All @@ -419,12 +429,12 @@ public function testRenderCacheDynamicActiveOnSecondRender(string $slug, array $
$cache = $this->app->cache('pages');
$page = $this->app->page($slug);

$this->assertNull($cache->retrieve($slug . '.html'));
$this->assertNull($cache->retrieve($slug . '.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$cacheValue = $cache->retrieve($slug . '.html');
$cacheValue = $cache->retrieve($slug . '.latest.html');
$this->assertNotNull($cacheValue);
$this->assertSame(in_array('auth', $dynamicElements), $cacheValue->value()['usesAuth']);
if (in_array('cookie', $dynamicElements)) {
Expand Down Expand Up @@ -454,12 +464,12 @@ public function testRenderCacheDataInitial()
$cache = $this->app->cache('pages');
$page = $this->app->page('data');

$this->assertNull($cache->retrieve('data.html'));
$this->assertNull($cache->retrieve('data.latest.html'));

$html = $page->render(['test' => 'custom test']);
$this->assertStringStartsWith('This is a custom test:', $html);

$this->assertNull($cache->retrieve('data.html'));
$this->assertNull($cache->retrieve('data.latest.html'));
}

/**
Expand All @@ -471,12 +481,12 @@ public function testRenderCacheDataPreCached()
$cache = $this->app->cache('pages');
$page = $this->app->page('data');

$this->assertNull($cache->retrieve('data.html'));
$this->assertNull($cache->retrieve('data.latest.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$value = $cache->retrieve('data.html');
$value = $cache->retrieve('data.latest.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($html1, $value->value()['html']);
$this->assertNull($value->expires());
Expand All @@ -485,7 +495,7 @@ public function testRenderCacheDataPreCached()
$this->assertStringStartsWith('This is a custom test:', $html2);

// cache still stores the non-custom result
$value = $cache->retrieve('data.html');
$value = $cache->retrieve('data.latest.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($html1, $value->value()['html']);
$this->assertNull($value->expires());
Expand Down Expand Up @@ -595,13 +605,6 @@ public function testRenderHookAfter()
*/
public function testRenderVersionDetectedFromRequest()
{
// TODO: To be removed in the next PR when caching respects versions
$this->app = $this->app->clone([
'options' => [
'cache.pages' => false
]
]);

$page = $this->app->page('version');
$page->version('latest')->save(['title' => 'Latest Title']);
$page->version('changes')->save(['title' => 'Changes Title']);
Expand All @@ -623,13 +626,6 @@ public function testRenderVersionDetectedFromRequest()
*/
public function testRenderVersionDetectedRecursive()
{
// TODO: To be removed in the next PR when caching respects versions
$this->app = $this->app->clone([
'options' => [
'cache.pages' => false
]
]);

$versionPage = $this->app->page('version');
$versionPage->version('latest')->save(['title' => 'Latest Title']);
$versionPage->version('changes')->save(['title' => 'Changes Title']);
Expand Down Expand Up @@ -658,13 +654,6 @@ public function testRenderVersionDetectedRecursive()
*/
public function testRenderVersionManual()
{
// TODO: To be removed in the next PR when caching respects versions
$this->app = $this->app->clone([
'options' => [
'cache.pages' => false
]
]);

$page = $this->app->page('version');
$page->version('latest')->save(['title' => 'Latest Title']);
$page->version('changes')->save(['title' => 'Changes Title']);
Expand All @@ -683,13 +672,6 @@ public function testRenderVersionManual()
*/
public function testRenderVersionException()
{
// TODO: To be removed in the next PR when caching respects versions
$this->app = $this->app->clone([
'options' => [
'cache.pages' => false
]
]);

$page = $this->app->page('version-exception');

try {
Expand Down

0 comments on commit e79cc36

Please sign in to comment.