From 754a49973613cfd8f2c08e9f309b4517c11fe85b Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Wed, 24 Dec 2025 12:32:10 -0500 Subject: [PATCH] Return `null` duration instead of default from stopCollectingCacheInfo --- src/services/Elements.php | 10 ---------- src/services/TemplateCaches.php | 17 ++++++++--------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/services/Elements.php b/src/services/Elements.php index 31dab7fbe99..c4109b93d35 100644 --- a/src/services/Elements.php +++ b/src/services/Elements.php @@ -691,16 +691,6 @@ public function stopCollectingCacheInfo(): array return [null, null]; } - // Only use the duration if it's less than the cacheDuration config setting - $generalConfig = Craft::$app->getConfig()->getGeneral(); - if ($generalConfig->cacheDuration) { - if ($duration) { - $duration = min($duration, $generalConfig->cacheDuration); - } else { - $duration = $generalConfig->cacheDuration; - } - } - $dep = new TagDependency([ 'tags' => array_keys($tags), ]); diff --git a/src/services/TemplateCaches.php b/src/services/TemplateCaches.php index 7fc2369d79a..7fbbf4df047 100644 --- a/src/services/TemplateCaches.php +++ b/src/services/TemplateCaches.php @@ -156,6 +156,8 @@ public function endTemplateCache(string $key, bool $global, ?string $duration, m } [$dep, $maxDuration] = Craft::$app->getElements()->stopCollectingCacheInfo(); + $defaultDuration = Craft::$app->getConfig()->getGeneral()->cacheDuration; + $maxDuration = min($maxDuration, $defaultDuration); if ($withResources) { $view = Craft::$app->getView(); @@ -174,21 +176,18 @@ public function endTemplateCache(string $key, bool $global, ?string $duration, m $saveCache = !StringHelper::contains(stripslashes($body), 'assets/generate-transform'); if ($saveCache) { - if (!$dep) { - $dep = new TagDependency(); - } + $dep = $dep ?? new TagDependency(); // Always add a `template` tag $dep->tags[] = 'template'; + $cacheInfo = [ + 'tags' => $dep->tags, + ]; + if ($maxDuration) { $expiryDate = DateTimeHelper::now()->modify("+$maxDuration seconds"); - $cacheInfo = [ - 'tags' => $dep->tags, - 'expiryDate' => DateTimeHelper::toIso8601($expiryDate), - ]; - } else { - $cacheInfo = $dep->tags; + $cacheInfo['expiryDate'] = DateTimeHelper::toIso8601($expiryDate); } $cacheValue = [$body, $cacheInfo];