From e1db9da034ee11ca57566ebd689daec2fff7d7c6 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 28 Aug 2026 12:24:54 -0400 Subject: [PATCH 1/2] Preserve nested cache expiry dates --- src/services/Elements.php | 2 +- tests/unit/services/ElementsTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/services/Elements.php b/src/services/Elements.php index 4876e3ba6e2..4c43f1055a4 100644 --- a/src/services/Elements.php +++ b/src/services/Elements.php @@ -781,7 +781,7 @@ public function stopCollectingCacheInfo(): array // Override the parent duration if ours is shorter $this->_cacheDuration = array_pop($this->_cacheDurationBuffers); - if ($duration && $duration < $this->_cacheDuration) { + if ($duration && (!$this->_cacheDuration || $duration < $this->_cacheDuration)) { $this->_cacheDuration = $duration; } } else { diff --git a/tests/unit/services/ElementsTest.php b/tests/unit/services/ElementsTest.php index e567e77dd62..a4c41821fcb 100644 --- a/tests/unit/services/ElementsTest.php +++ b/tests/unit/services/ElementsTest.php @@ -12,6 +12,7 @@ use Craft; use craft\elements\Entry; +use craft\helpers\DateTimeHelper; use craft\services\Elements; use craft\test\TestCase; use craft\test\TestSetup; @@ -83,6 +84,20 @@ public function testParseRefs(): void } } + public function testNestedCacheInfoPreservesExpiry(): void + { + $this->elements->startCollectingCacheInfo(); + $this->elements->collectCacheTags(['test']); + $this->elements->startCollectingCacheInfo(); + $this->elements->setCacheExpiryDate(DateTimeHelper::now()->modify('+60 seconds')); + $this->elements->stopCollectingCacheInfo(); + [, $duration] = $this->elements->stopCollectingCacheInfo(); + + self::assertNotNull($duration); + self::assertGreaterThan(0, $duration); + self::assertLessThanOrEqual(60, $duration); + } + /** * @inheritdoc */ From dabb0f9a720e8aae77ec3d607805f6ce39e57762 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 28 Aug 2026 12:34:20 -0400 Subject: [PATCH 2/2] Clarify nested cache duration handling --- src/services/Elements.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/Elements.php b/src/services/Elements.php index 4c43f1055a4..8886c13eafd 100644 --- a/src/services/Elements.php +++ b/src/services/Elements.php @@ -779,7 +779,7 @@ public function stopCollectingCacheInfo(): array if (!empty($this->_cacheTagBuffers)) { $this->_cacheTags = array_merge(array_pop($this->_cacheTagBuffers), $tags); - // Override the parent duration if ours is shorter + // Preserve the shortest duration across nested collections $this->_cacheDuration = array_pop($this->_cacheDurationBuffers); if ($duration && (!$this->_cacheDuration || $duration < $this->_cacheDuration)) { $this->_cacheDuration = $duration;